Labs ICT
โญ Pro Login

Data Types

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.

๐Ÿงช Quick Quiz

Which C++ data type would you use to store a single character?