2.2 — Whole numbers (int)
Read one box at a time. No rush.
int#
int holds whole numbers: no decimals. They can be negative.
C++
int temperature { -5 };int year { 2026 };How big can an int be?#
An int is usually 4 bytes. That holds numbers from about -2.1 billion to +2.1 billion.
Need bigger? Use long long:
C++
long long worldPopulation { 8'100'000'000 };Tip
You can put ' inside long numbers to make them easier to read. 8'100'000'000 is the same as 8100000000.
Overflow#
If a number gets too big for its type, it overflows. The result is wrong, and for int it is undefined behavior.
C++ — wrong ✗
int big { 2'147'483'647 }; // the biggest intbig = big + 1; // overflow! do not do thisWatch out
Pick a type big enough for your numbers. For counting things that may get huge, use long long.
unsigned (only zero and up)#
unsigned int cannot hold negative numbers. Beginners should mostly avoid it: subtracting below zero wraps around to a giant number.
C++
unsigned int a { 2 };unsigned int b { 3 };std::cout << a - b; // prints 4294967295, not -1!Good habit
Use plain int for normal numbers, even ones that "can't be negative".
Tip: press ← / → to move between lessons, / to search.