C++ has several built-in data types, and each one serves a different purpose. Choosing the right type matters because it affects memory usage and performance. Here are the ones you will use the most.
Basic Data Types
intโ whole numbers like 42, -7, 0. Uses 4 bytes.doubleโ decimal numbers like 3.14, -0.001, 99.9. Uses 8 bytes.floatโ decimal numbers with less precision, uses 4 bytes.charโ single characters like 'A', 'z', '5'. Uses 1 byte.boolโ true or false. Uses 1 byte.
Size of Types
You can check how much memory a type uses with the sizeof() operator.
Sizes can vary depending on your system architecture, but these are the typical values.
#include <iostream>
using namespace std;
int main() {
int age = 21;
double price = 45.99;
float pi = 3.14f;
char letter = 'B';
bool done = false;
cout << "int: " << sizeof(age) << " bytes" << endl;
cout << "double: " << sizeof(price) << " bytes" << endl;
cout << "float: " << sizeof(pi) << " bytes" << endl;
cout << "char: " << sizeof(letter) << " bytes" << endl;
cout << "bool: " << sizeof(done) << " bytes";
return 0;
}
Notice the f after the float value โ that tells C++ it is a float,
not a double. Without it, C++ treats decimal numbers as doubles by default.