Fundamental Types: int, double, char, bool
When you declare a variable in C++, you declare its type upfront, and that type is fixed for the life of the variable. The compiler uses this information to decide how much memory to reserve, which operations make sense on it, and how to interpret the raw bytes sitting in that memory.
There are four types you’ll use constantly: int for whole numbers, double for decimal numbers, char for single characters, and bool for true/false values. This lesson covers all four.
int: Whole numbers
You’ve already seen int in action. To recap: it holds whole numbers that are positive, negative, or zero.
double: Decimal numbers
When you need a fractional part, you use double. The name comes from “double-precision floating-point,” which refers to how the value is stored internally. You don’t need to worry about that yet, just know it can represent a wide range of decimal values with reasonable precision.
char: Single characters
char holds a single character: a letter, digit, punctuation mark, or whitespace. You write character literals using single quotes:
Some characters are invisible or have a special meaning that makes them awkward to write directly. C++ handles these with escape sequences: a backslash followed by a letter that together represent a single character. Here are the ones you’ll likely use a lot:
| Escape | Meaning |
|---|---|
'\n' | Newline (moves to the next line) |
'\t' | Tab |
'\\' | A literal backslash |
'\'' | A literal single quote |
The backslash is what signals “this is an escape sequence.” That’s also why a literal backslash needs to be written as '\\': the first backslash says “something special follows,” and the second one says “it’s a backslash.” And '\'' is how you write a single quote inside a char literal, since a bare ' would end the literal prematurely.
In practice, when printing text you’ll use strings in double quotes, not a chain of individual characters. You’ve probably noticed "\n" written as a separate item in earlier snippets. It can also be embedded directly in the string:
std::cout << "cat\n"; // same as: std::cout << "cat" << "\n";This just moves the cursor to the next line after printing cat.
Under the hood, char is just a small integer. Each character has a numeric code defined by the ASCII standard. For example, 'A' is 65, 'a' is 97, and '0' is 48. This means you can do arithmetic on characters:
Line 5 adds 1 to the numeric code that represents 'a' and stores it in the variable next. Since 'a' is 97, next holds 98, which C++ interprets as 'b' when printing.
Arithmetic on characters is occasionally useful but easy to misuse, so most of the time you’ll just use char to store a single character.
Single quotes vs. double quotes:
'A'is achar."A"is a string (specifically, a C-style string. More on this later). They’re different types and not interchangeable. If you accidentally use double quotes for a value and assign it to acharvariable, you’ll get a compiler warning or error.
bool: True or false
bool stores a boolean value: either true or false.
bool isLoggedIn = false;bool hasDiscount = true;By default, std::cout prints true as 1 and false as 0. If you want the words true and false to be printed, you can use std::boolalpha (as shown below). Try removing it from the snippet to see how the output changes.
Internally, bool is stored as an integer where 0 means false and anything non-zero means true. This means you can accidentally assign a number to a bool and C++ won’t stop you:
bool flag = 42; // valid, treated as truebool off = 0; // valid, treated as falseThis is technically fine but usually not what you want. Prefer explicit true/false literals.
Static typing
When someone says C++ is statically typed, this is what they mean: the type of every variable is known at compile time. Languages like Python and JavaScript work differently. A variable’s type in these languages is only figured out at runtime, and can change.
The strictness in C++ pays off as many bugs become compile-time errors instead of runtime errors surprising the user of an application you developed. If you accidentally try to store "hello" in an int type of variable, the compiler tells you immediately rather than letting your program fall apart later in unpredictable ways.