Variables and fundamental types
A variable is a name tied to a piece of memory that holds a value. In C++ every variable has a fixed type chosen when you declare it, and that type decides how much memory it uses and which operations are allowed. This discipline, called static typing, lets the compiler catch many mistakes before the program runs. It is one of the big differences from languages like Python, where a variable can freely change type.
The basic types
- int: whole numbers, normally 32 bits (about -2 billion to 2 billion).
- long long: integers of at least 64 bits, for large values.
- double: double-precision decimal numbers (the default for reals).
- float: single-precision decimals, uses less memory but is less exact.
- char: a single character, stored as a small integer.
- bool: true or false.
- unsigned: variants without sign that only hold zero or positive values.
#include <iostream>
int main() {
int age = 30;
double price = 19.99;
char grade = 'A';
bool active = true;
long long big = 9000000000LL;
std::cout << "age: " << age << '\n';
std::cout << "price: " << price << '\n';
std::cout << "grade: " << grade << '\n';
std::cout << "active: " << std::boolalpha << active << '\n';
std::cout << "big: " << big << '\n';
return 0;
}Notice that each declaration has the form type name = value;. Literals also have a type: 19.99 is a double, 'A' is a char and the LL suffix marks a long long. std::boolalpha makes booleans print as true or false instead of 1 or 0. Names may contain letters, digits and underscores, but not start with a digit, and C++ is case sensitive: age and Age are different variables.
Initialization: the danger of uninitialized values
If you declare int x; inside a function without giving it a value, x holds garbage: whatever was in that memory. Reading it is undefined behavior, a bug that may work a thousand times and fail on the next. The modern, safe way is brace initialization, int x{0};, which also forbids conversions that lose information: int n{3.5}; does not compile, while int n = 3.5; compiles silently and stores 3. Get used to always giving every variable an initial value.
#include <iostream>
#include <limits>
int main() {
int a{7};
double ratio{0.25};
int truncated = static_cast<int>(9.99); // explicit conversion: 9
std::cout << "sizeof(int) = " << sizeof(int) << " bytes\n";
std::cout << "max int = " << std::numeric_limits<int>::max() << '\n';
std::cout << "a / 2 = " << a / 2 << '\n'; // integer division: 3
std::cout << "a / 2.0 = " << a / 2.0 << '\n'; // real division: 3.5
std::cout << "truncated = " << truncated << '\n';
std::cout << "ratio = " << ratio << '\n';
return 0;
}Two classic traps
The first is integer division: when both operands are int, the result is an int too and the decimal part is discarded, so 7 / 2 is 3. To get 3.5 at least one operand must be a double. The second is overflow: adding one to the maximum value of a signed int is undefined behavior. If you need big numbers, pick long long from the start. To convert between types explicitly use static_cast<type>(value); it states your intention and is easy to search for in the code.
✎ ملاحظاتي
محفوظ في هذا المتصفح فقط. سجّل الدخول لحفظه في حسابك.0/5000 حرف