Quiz
Warm-up from lesson 1-3: you fixed `int name = "Grace";`. Why did the original line fail to compile?
Every C++ type has a fixed size in memory. That is the deal that makes the language fast: the compiler knows at build time exactly how many bytes each variable needs, so it can lay out memory and emit raw CPU instructions with no runtime type checks. It also means choosing a type is a real engineering decision — in 1996 the Ariane 5 rocket was destroyed seconds after launch partly because a value was converted into an integer type too small to hold it. This lesson gives you the five types that cover nearly all the code you will write.
The five types you will use constantly
| Type | Holds | Example |
|---|---|---|
int | whole numbers (about ±2.1 billion) | int n = 42; |
double | decimal numbers (64-bit floating point) | double t = 9.81; |
bool | true or false | bool ok = true; |
char | one single character, in 'single quotes' | char grade = 'A'; |
std::string | text of any length, in "double quotes" | std::string s = "hi"; |
Notes for Python programmers:
- Python's
intgrows without limit. C++'sintis a fixed 32-bit box. If a value can exceed ~2.1 billion (common in interview math), uselong long, a 64-bit int. floatalso exists but is less precise thandouble. Default todouble.charandstd::stringare different types:'A'is one character,"A"is a string containing one character.std::stringneeds#include <string>.
Code exercise · cpp
Run this tour of the core types. Note how bool prints as 1 by default, and as true after std::boolalpha.
Worked example: when int is not enough
The sum 1 + 2 + ... + n equals n(n + 1)/2. For n = 100,000 that is 5,000,050,000 — bigger than an int's ~2.1 billion ceiling. If you compute it in int arithmetic, the true value cannot fit in the 32-bit box and the stored result is garbage (signed overflow is undefined behavior — the compiler is allowed to assume it never happens). Declaring the variables long long makes every step of the arithmetic 64-bit:
long long n = 100000; long long sum = n * (n + 1) / 2; // 5000050000, fits comfortably
The rule interviewers expect you to know: before summing or multiplying, estimate the biggest possible value. Anything that can pass ~2 × 10⁹ goes in long long.
Code exercise · cpp
Run the 64-bit sum. Then, in your head: what would happen if both variables were plain int? (Answer: the multiplication overflows before the division can save it.)
const: values that never change
Mark a variable const and the compiler forbids every later assignment to it:
const double PI = 3.14159; PI = 3.2; // COMPILE ERROR: assignment of read-only variable
Python has the ALL_CAPS naming convention for constants, but nothing stops you from reassigning them. In C++, const is enforced by the compiler, just like the type checking you met in lesson 1-3.
Use const on everything that should not change. It documents intent, prevents accidental writes, and later in the course it becomes essential when passing big objects to functions cheaply and safely.
Code exercise · cpp
Your turn. Declare a const double named PI with value 3.14159 and a double named radius read from input. Print the circle's area (PI × radius × radius). With input `2.0` the output must be exactly `Area: 12.5664`.