Every value stored in a computer's memory must have a clearly defined Data Type. The data type instructs the compiler how many bytes of RAM to reserve and how to interpret the binary bit patterns stored inside.
int, char, float, double, bool, void.struct, class, union, enum, typedef / using.| Data Type | Size (Bytes) | Bits | Approximate Value Range | Typical Real-World Use Case |
|---|---|---|---|---|
bool |
1 | 8 | true (1) or false (0) |
Flags, logic conditionals |
char |
1 | 8 | -128 to 127 (or 0 to 255 unsigned) | Single ASCII character ('A') |
short |
2 | 16 | -32,768 to 32,767 | Small numbers, audio samples |
int |
4 | 32 | -2.14 Billion to +2.14 Billion | General-purpose counters, loop indices |
unsigned int |
4 | 32 | 0 to 4,294,967,295 (~4.29 Billion) | Quantities that cannot be negative (sizes) |
long long |
8 | 64 | -(2^63) to 2^63 - 1 (~9 Quintillion) | Large IDs, database keys, high-precision time |
float |
4 | 32 | ~1.18e-38 to 3.40e+38 (7 digits precision) | Graphics shaders, physics engines |
double |
8 | 64 | ~2.23e-308 to 1.79e+308 (15-17 digits) | Scientific simulations, financial math |
sizeof#include <iostream>
int main() {
std::cout << "Size of bool: " << sizeof(bool) << " byte\n";
std::cout << "Size of char: " << sizeof(char) << " byte\n";
std::cout << "Size of int: " << sizeof(int) << " bytes\n";
std::cout << "Size of long long: " << sizeof(long long) << " bytes\n";
std::cout << "Size of double: " << sizeof(double) << " bytes\n";
return 0;
}
Boost your C++ proficiency by studying these closely connected core modules:
Understanding variable naming and initialization.
Converting safely between different types.
Fixed-size container data types.