In C++, variables must be declared with a specific data type. This tells the compiler how much memory to allocate and what kind of data the variable will hold.
Here are the most common built-in data types:
| Data Type | Size | Description |
|---|---|---|
int |
4 bytes | Stores whole numbers, e.g., 100, -50 |
float |
4 bytes | Stores fractional numbers with 6-7 decimal digits |
double |
8 bytes | Stores fractional numbers with 15 decimal digits |
char |
1 byte | Stores a single character, e.g., 'a', 'Z' |
bool |
1 byte | Stores true or false |
Use int when you don't need decimals. Use double (or float) when you need precision for floating-point values.
int myNum = 1000; float myFloat = 5.75f; double myDouble = 19.99;
Understanding data types is essential for memory optimization and preventing unexpected errors in your programs.
Which data type should you use to store a single character like 'A'?