Fundamental Data Types
The built-in (fundamental) types
| Type | Typical size | Holds | Example |
|---|---|---|---|
bool | 1 byte | true / false | bool paid = true; |
char | 1 byte | one character | char grade = 'A'; |
short | 2 bytes | small integer | short year = 2026; |
int | 4 bytes | integer (≈ ±2.1 billion) | int count = 42; |
long long | 8 bytes | big integer (≈ ±9.2 × 10¹⁸) | long long views = 9000000000; |
float | 4 bytes | decimal, ~7 digits precision | float temp = 36.6f; |
double | 8 bytes | decimal, ~15–16 digits ✅ default | double price = 99.99; |
long double | 8–16 bytes | extended precision |
Sizes can vary between platforms; check with sizeof.
signed vs unsigned
intcan be negative;unsigned intcannot but goes twice as high- Mixing signed and unsigned in comparisons causes surprising bugs ⚠️
Literals
| Literal | Type |
|---|---|
42 | int |
42LL | long long |
42u | unsigned |
3.14 | double |
3.14f | float |
'A' | char |
"text" | string literal (array of const char) |
true | bool |
0x1F, 0b1010, 017 | hex, binary (C++14), octal |
1'000'000 | digit separators (C++14) |
bool printing
std::cout << true prints 1. Use std::boolalpha to print true/false.
char is a small integer
'A' is stored as 65 (ASCII). char c = 'A' + 1; gives 'B'.
Which type should I use?
- Whole numbers:
int(orlong longfor big values) - Decimals:
double - Yes/no:
bool - Text:
std::string(Unit 3) - Money: integers in the smallest unit (paisa/cents) to avoid rounding
Example
#include <iostream>
int main() {
bool is_paid = true;
char grade = 'A';
int lessons = 130;
long long downloads = 9'000'000'000LL;
double price = 499.99;
float rating = 4.5f;
std::cout << "sizeof(bool) = " << sizeof(bool) << '\n';
std::cout << "sizeof(char) = " << sizeof(char) << '\n';
std::cout << "sizeof(int) = " << sizeof(int) << '\n';
std::cout << "sizeof(long long) = " << sizeof(long long) << '\n';
std::cout << "sizeof(double) = " << sizeof(double) << '\n';
std::cout << "is_paid: " << is_paid << " / " << std::boolalpha << is_paid << '\n';
std::cout << "grade: " << grade << ", next grade code: " << grade + 1
<< ", as char: " << static_cast<char>(grade + 1) << '\n';
std::cout << lessons << " lessons, " << downloads << " downloads\n";
std::cout << "price " << price << ", rating " << rating << '\n';
std::cout << "hex 0xFF = " << 0xFF << ", binary 0b1010 = " << 0b1010 << '\n';
return 0;
}
/* Output:
sizeof(bool) = 1
sizeof(char) = 1
sizeof(int) = 4
sizeof(long long) = 8
sizeof(double) = 8
is_paid: 1 / true
grade: A, next grade code: 66, as char: B
130 lessons, 9000000000 downloads
price 499.99, rating 4.5
hex 0xFF = 255, binary 0b1010 = 10
*/