2.1 — What is a type?

📘 Chapter 2⏱ about 1 min read✍️ Beginner friendly
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#

TypeWhat it holdsExample
intwhole numbers-3, 0, 250
doubledecimal numbers3.14, -0.5
booltrue or falsetrue, false
charone character'a', '7', '!'
std::stringtext (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.)

Finished reading? Tick it off. Your progress is saved in this browser.

Tip: press ← / → to move between lessons, / to search.