Variables, Data Types & Operators
auto, the arithmetic/comparison/logical operator families, assignment shorthand, increment/decrement, and the classic integer-vs-floating-point division gotcha that trips up almost every beginner.
Declaring & Initializing Variables
A variable is a named piece of memory that holds a value. In C++, every variable must be declared with a type — this is different from Python, where a variable's type is only decided when a value is assigned to it. In C++, the type is fixed at the point of declaration and the compiler enforces it.
// variables.cpp // Declaring and printing variables of different types #include <iostream> #include <string> int main() { int age = 25; double price = 19.99; float temperature = 36.6f; char grade = 'A'; bool isStudent = true; std::string name = "Maria"; std::cout << "Name: " << name << std::endl; std::cout << "Age: " << age << std::endl; std::cout << "Price: " << price << std::endl; std::cout << "Temperature: " << temperature << std::endl; std::cout << "Grade: " << grade << std::endl; std::cout << "Is student: " << isStudent << std::endl; return 0; }
int age; declares a variable without giving it a value — it holds garbage memory until assigned. int age = 25; declares and initializes it in one step. Always initialize your variables; reading an uninitialized variable is undefined behavior in C++.std::cout prints bool values as 1 (true) or 0 (false), not the words "true"/"false". You'll see this in the output of the example above for isStudent.C++'s Primitive Data Types
C++ gives you fine-grained control over exactly what kind of data a variable stores — and how much memory it uses. The sizes below are the typical values on most modern 32/64-bit desktop platforms; the C++ standard technically leaves exact sizes implementation-defined, so they can vary on unusual hardware or embedded compilers.
f suffix, like 36.6f.'A'. Internally stored as a small integer code.true or false. Typically occupies 1 byte in memory even though it only needs 1 bit of information.<string> that manages a growable sequence of characters for you.char array of a million elements uses roughly a quarter of the memory an equivalent int array would. In performance-sensitive or embedded code, these choices genuinely matter.Type Inference with auto
Writing out long type names repeatedly can get tedious. The auto keyword tells the compiler: "figure out the type yourself, from whatever I initialize this variable with." The type is still fixed at compile time — auto is not dynamic typing, it's just a shortcut for the compiler to do the typing for you.
// auto_demo.cpp #include <iostream> #include <string> int main() { auto x = 10; // deduced as int auto y = 3.14; // deduced as double auto name = std::string("Alex"); // deduced as std::string std::cout << x << " " << y << " " << name << std::endl; return 0; }
auto x; with no value on the right is a compile error — there's nothing for the compiler to infer the type from. auto only works when you initialize the variable on the same line you declare it.Arithmetic Operators
C++ supports the standard arithmetic operators: + (add), - (subtract), * (multiply), / (divide), and % (modulo — the remainder after division).
// arithmetic.cpp #include <iostream> int main() { int a = 7; int b = 2; std::cout << "a + b = " << (a + b) << std::endl; std::cout << "a - b = " << (a - b) << std::endl; std::cout << "a * b = " << (a * b) << std::endl; std::cout << "a / b = " << (a / b) << std::endl; // integer division -> 3 std::cout << "a % b = " << (a % b) << std::endl; // remainder -> 1 double da = 7.0; double db = 2.0; std::cout << "da / db = " << (da / db) << std::endl; // 3.5 return 0; }
/ are integers, C++ performs integer division — the result is truncated toward zero, so 7 / 2 gives 3, not 3.5. To get a decimal result, at least one operand must be a floating-point type (double or float). This catches almost every C++ beginner at least once.Comparison & Logical Operators
Comparison operators (==, !=, <, >, <=, >=) compare two values and produce a bool. Logical operators combine or invert boolean values: && (AND — true only if both sides are true), || (OR — true if either side is true), and ! (NOT — flips true/false).
// logic.cpp #include <iostream> int main() { int age = 20; bool hasID = true; std::cout << (age >= 18) << std::endl; // 1 (true) std::cout << (age >= 18 && hasID) << std::endl; // 1 (true) std::cout << (age < 18 || !hasID) << std::endl; // 0 (false) return 0; }
= is the assignment operator — it stores a value. A double == is the equality comparison — it checks if two values are equal. Writing if (age = 18) instead of if (age == 18) is a classic bug: it silently assigns 18 to age instead of comparing.Assignment Shorthand & Increment/Decrement
C++ offers compound assignment operators that combine an arithmetic operation with assignment: +=, -=, *=, /=, %=. It also has dedicated increment (++) and decrement (--) operators for adding or subtracting exactly 1.
// shorthand.cpp #include <iostream> int main() { int score = 10; score += 5; // same as: score = score + 5; -> 15 score -= 2; // same as: score = score - 2; -> 13 score *= 2; // same as: score = score * 2; -> 26 score /= 4; // same as: score = score / 4; -> 6 (integer division) std::cout << "score = " << score << std::endl; int counter = 0; counter++; // counter is now 1 ++counter; // counter is now 2 std::cout << "counter = " << counter << std::endl; return 0; }
++counter (pre-increment) increments first, then evaluates to the new value. counter++ (post-increment) evaluates to the old value, then increments. When the result isn't used in the same expression — as in the example above — they behave identically. The difference only matters when you use the result immediately, like int x = counter++; vs int x = ++counter;.Lesson Summary
Let's recap everything you learned in this lesson:
int, double, float, char, bool — plus std::string from the Standard Library. Sizes shown are typical, not guaranteed by the standard.auto lets the compiler infer a variable's type from its initializer — it's still statically typed, just less typing for you.7 / 2 is 3, not 3.5. Use a floating-point operand to get a decimal result.bool; &&, ||, and ! combine or invert them.+=, -=...) and increment/decrement (++, --) are shorthand for common update patterns.int on most modern desktop platforms?auto x = 5; do?7 / 2 when both operands are int?x += 3; do?Now it's your turn to write real code. Compile and run it with g++ or an online compiler.
Write a program called
rectangle.cpp that declares the length and width of a rectangle as double variables (pick any values you like, e.g. 7.5 and 3.0), then computes and prints:
Width: 3
Area: 22.5
Perimeter: 21
Rules: Area = length × width. Perimeter = 2 × (length + width). Use
double for all four values so division/multiplication stays precise. Print each value with its own std::cout line.
💡 Show hints if you're stuck
- Declare:
double length = 7.5;anddouble width = 3.0; - Compute area:
double area = length * width; - Compute perimeter:
double perimeter = 2 * (length + width); - Print with
std::cout << "Area: " << area << std::endl;