C++ is a statically typed language, meaning every variable must have a declared type. The type determines what kind of data is stored and how much memory it uses.
Integer Types
Type
Size
Range
short
2 bytes
-32,768 to 32,767
int
4 bytes
-2,147,483,648 to 2,147,483,647
long
4–8 bytes
at least 32-bit range
long long
8 bytes
±9.2 × 10¹⁸
Floating-Point Types
Type
Size
Precision
float
4 bytes
~6–7 decimal digits
double
8 bytes
~15–16 decimal digits
long double
8–16 bytes
even higher precision
#include <iostream>
using namespace std;
int main() {
int a = 1000000;
double b = 3.14159265358979;
float c = 3.14f; // Use 'f' suffix for float literals
char d = 'Z';
bool e = false;
cout << "int: " << a << endl;
cout << "double: " << b << endl;
cout << "float: " << c << endl;
cout << "char: " << d << endl;
cout << "bool: " << e << endl;
return 0;
}
Output:
int: 1000000
double: 3.14159265358979
float: 3.14
char: Z
bool: 0
If you store a value too large for its type, overflow occurs — the value wraps around unexpectedly:
short x = 32767;
x++;
cout << x; // Output: -32768 (overflow!)
Warning: Always choose a data type large enough to hold the values you expect.
Exercise
Declare variables for: the population of Earth (use long long), the speed of light in m/s (use double), the first letter of the alphabet (use char). Print all three.