Variable & C++ Data Types (int, double, char, bool)
A variable is a named container in RAM that holds a value which can change while a program runs. In C++, every variable must be declared with a specific data type before use.
How It Works & Why It Matters
The data type defines how much memory is allocated and what values are valid.
In middle-school C++, the essential types are int (integers), long long (large integers), double (decimals), char (single character), and bool (true/false).
int age = 13; // Integer long long id = 51103150001; // Large integer double avg = 9.75; // Decimal number char grade = 'A'; // Single character bool isEven = (n % 2 == 0); // true or false
๐ซ Classroom Example
Writing int sum = 0; ensures your accumulator starts cleanly at zero instead of holding random leftover memory.
โ๏ธ int vs. double
int keeps exact whole numbers and drops any fractional part; double preserves decimals accurately for averages and geometry.
Storing division results like 7 / 2 in an int variable (gives 3 instead of 3.5), or forgetting to initialize counters and sums.