A data type is a classification that tells the compiler:
C++ provides several fundamental (primitive) data types built into the language.
int)Used to store whole numbers (no decimal part).
| Modifier | Size | Range |
|---|---|---|
short int | 2 bytes | -32,768 to 32,767 |
int | 4 bytes | -2,147,483,648 to 2,147,483,647 |
long int | 4–8 bytes | Platform dependent |
long long int | 8 bytes | Very large range |
signed (default): stores both positive and negative valuesunsigned: stores only non-negative values (0 and above), doubling the positive rangeint a = -10; // signed (default)
unsigned int b = 200; // unsigned, range: 0 to 4,294,967,295
Used to store real numbers (numbers with fractional/decimal parts).
| Type | Size | Precision |
|---|---|---|
float | 4 bytes | ~7 decimal digits |
double | 8 bytes | ~15 decimal digits |
long double | 10–16 bytes | Extended precision |
float pi = 3.14f;
double precise_pi = 3.14159265358979;
Use double when higher precision is needed (e.g., scientific calculations).
char)Used to store a single character using its ASCII code.
char grade = 'A'; // stored as ASCII value 65
char newline = '\n'; // escape character
ASCII Examples:
'A' = 65, 'a' = 97, '0' = 48bool)Used to store logical values.
true (stored as 1) or false (stored as 0)bool isLoggedIn = true;
bool hasError = false;
if (isLoggedIn) {
cout << "Welcome!";
}
void)void means no type or no value. It is used:
void*) that can point to any data typevoid printMessage() { // returns nothing
cout << "Hello";
}
void *ptr1; // generic pointer — can point to any type
int *ptr2; // typed pointer — can only point to int
void* vs int*| Feature | void *ptr1 | int *ptr2 |
|---|---|---|
| Points to | Any data type | Only int variables |
| Dereferencing | Requires explicit cast | Direct dereferencing allowed |
| Type safety | Less type-safe | Type-safe |
| Use case | Generic functions, malloc | Integer-specific operations |
| Data Type | Size | Example Values |
|---|---|---|
int | 4 bytes | -100, 0, 42 |
unsigned int | 4 bytes | 0, 200, 65535 |
float | 4 bytes | 3.14, -0.5 |
double | 8 bytes | 3.14159265 |
char | 1 byte | 'A', 'z', '5' |
bool | 1 byte | true, false |
void | — | (no value) |