2.1 — What is a type?
Read one box at a time. No rush.
Everything is 0s and 1s#
Inside the computer, every value is stored as 0s and 1s, called bits. Eight bits make one byte.
The pattern 01000001 could mean the number 65, or the letter A. How does the computer know which?
The type tells it.
Key idea
A type tells C++ two things: how much memory a value needs, and how to read the 0s and 1s.
The basic types#
| Type | What it holds | Example |
|---|---|---|
int | whole numbers | -3, 0, 250 |
double | decimal numbers | 3.14, -0.5 |
bool | true or false | true, false |
char | one character | 'a', '7', '!' |
std::string | text (many characters) | "hello there" |
How big is a type?#
sizeof tells you how many bytes a type uses on your computer:
C++
std::cout << sizeof(int) << '\n';std::cout << sizeof(double) << '\n';std::cout << sizeof(char) << '\n';Output
481(Your numbers may be a bit different. They are usually these.)
Tip: press ← / → to move between lessons, / to search.