C++ data types: int, double, char, bool
Short answer: a type tells the compiler how many bytes a value needs and what those bytes mean. The four you'll live in as a beginner: int (whole numbers), double (decimals), char (one character — which is secretly a small number), bool (true/false, printed as 1/0).
All four, in one program
#include <iostream>
using namespace std;
int main() {
int age = 25; // whole numbers
double price = 9.99; // decimals
char grade = 'A'; // ONE character, single quotes
bool ok = true; // true or false
cout << age << " " << price << " " << grade << " " << ok << endl;
return 0;
}
Two things to notice: grade uses single quotes ('A', not "A" — that's a string, and mixing them is a classic compiler error), and ok prints as 1, not true — cout shows bools as numbers unless you ask for boolalpha.
The four types, one paragraph each
| Type | Stores | Typical size | Watch out for |
|---|---|---|---|
int | Whole numbers | 4 bytes | Overflow past ≈ ±2.1 billion; 7/2 is 3 |
double | Decimals | 8 bytes | 0.1 + 0.2 ≠ exactly 0.3 (floating-point fuzz) |
char | One character | 1 byte | 'a' vs "a"; it's secretly a number |
bool | true / false | 1 byte | Prints as 1/0; any nonzero number converts to true |
Sizes are "typical" because the C++ standard sets minimums, not exact values — on your desktop compiler, sizeof(int) prints the real answer for your machine.
char is a number wearing a costume
Every character is stored as an integer code (ASCII): 'A' is 65, 'a' is 97, '0' is 48. That's why you can do arithmetic on characters:
#include <iostream>
using namespace std;
int main() {
char c = 'A';
cout << c << " is code " << (int)c << endl;
cout << "next letter: " << (char)(c + 1) << endl;
cout << "digit '7' as number: " << '7' - '0' << endl;
return 0;
}
The last line — '7' - '0' — is a genuine interview-grade idiom: it converts a digit character into its numeric value. '7' has code 55 and '0' has code 48, so the subtraction leaves exactly 7 — and it works for every digit, no lookup table needed.
How each type breaks
- int breaks by wrapping. 2,147,483,647 + 1 doesn't error — it silently wraps to garbage (undefined behavior for signed ints). Our Factorial problem is capped at N ≤ 12 precisely because 13! overflows.
- double breaks by fuzz.
0.1 + 0.2prints something like0.3but compares not-equal to0.3. Never compare doubles with==; compare with a tolerance. And never store money in double — store cents in an int. - char breaks by quoting.
char c = "A";won't compile —"A"is a string. Single quotes for one character, always. - bool breaks by surprise conversions.
bool b = 5;compiles fine and istrue— any nonzero value converts to true. Handy in loops likewhile (cin >> x), sneaky elsewhere.
Choosing between int and double — the beginner decision tree
- Can the value ever have a fraction? (average, price, percentage) →
double. - Is it a count, index, year, score? →
int. - Do you need bigger than ±2.1 billion? →
long longon a desktop compiler (our playground covers the teaching core; see the compiler setup guide). - Money? → integer cents. Seriously.
Interview questions on types
- "What does cout print for a bool?" — 1 or 0;
cout << boolalphaswitches to true/false. - "Why is 7 / 2 equal to 3?" — both operands are ints, so the division is integer division (full guide: here).
- "What's the difference between 'A' and "A"?" — a char (one byte, code 65) vs a string (an array ending in
'\0'). - "What happens on int overflow?" — signed overflow is undefined behavior: no exception, no reliable wrap; the compiler may assume it never happens.
- "Why is char arithmetic useful?" — digit conversion (
'7' - '0'), case conversion (c - 'a' + 'A'), and it's the basis of how text processing works at all.
Practice
- In the playground: print your initials as chars, then their codes; add 32 to an uppercase char and see what happens (that's lowercase).
- Leap Year Check drills bool logic; Factorial meets the int ceiling head-on.
Related: integer division, pass by value vs pointer vs reference, 15 beginner interview questions.