In C++, Data Types are the classifications we give to data to tell the compiler how much memory to reserve and how to interpret the bit patterns stored in that memory. Without data types, the computer would see a sequence of 0s and 1s and wouldn't know if they represent a whole number, a decimal, or a letter.
C++ is a statically-typed language, meaning the type of a variable is determined at compile time and cannot change during the program's execution.
When you declare a variable with a specific data type, the compiler performs two critical tasks:
Memory Sizing: It reserves a specific number of bytes in the RAM.
Encoding: It decides how to store the data. For example, integers are stored using Two's Complement, while floating-point numbers follow the IEEE 754 Standard.
The int data type is used to store whole numbers (both positive and negative) without any fractional parts.
Theory: In most modern 64-bit systems, an int occupies 4 bytes (32 bits). This allows it to represent values from $-2,147,483,648$ to $2,147,483,647$.
Variation: You can use modifiers like short int (2 bytes), long int (8 bytes), or unsigned int (which only stores positive numbers, effectively doubling the positive range).
The float type is used for storing single-precision fractional numbers.
Theory: It typically occupies 4 bytes. It is suitable for storing decimals where high precision isn't the primary concern, as it provides about 7 decimal digits of precision.
Usage: Always append an f at the end of the literal (e.g., 3.14f) to tell the compiler it's a float rather than a double.
The double type is the default for decimal numbers in C++.
Theory: It occupies 8 bytes (64 bits). It provides "double" the precision of a float, offering about 15-17 decimal digits of precision.
Preference: In modern programming, double is preferred over float because modern CPUs process 64-bit doubles just as fast as 32-bit floats, and the extra precision prevents rounding errors in complex calculations.
The char data type stores a single character.
Theory: It occupies 1 byte (8 bits). Under the hood, C++ does not store the letter 'A'; it stores the ASCII value (which for 'A' is 65). Because it is technically an integer, you can perform math on characters.
Literals: Must be enclosed in single quotes (e.g., 'A', '9', '$').
The bool type represents logical values: true or false.
Theory: Conceptually, it only needs 1 bit, but in practice, it usually occupies 1 byte because memory is addressed in bytes.
Values: It can only hold true (internally represented as 1) or false (internally represented as 0).
| Data Type | Size (Typical) | Range (Approximate) | Purpose |
| bool | 1 Byte | true or false | Logical flags. |
| char | 1 Byte | -128 to 127 (or ASCII) | Individual characters. |
| int | 4 Bytes | -2.1 Billion to 2.1 Billion | Whole numbers / Counting. |
| float | 4 Bytes | $1.2E-38$ to $3.4E+38$ | Low-precision decimals. |
| double | 8 Bytes | $2.3E-308$ to $1.7E+308$ | High-precision decimals. |
#include <iostream> #include <iomanip> // For controlling decimal precision int main() { // Integer int players = 10; // Character (Single quotes) char rank = 'A'; // Floating points float temperature = 36.6f; double pi = 3.14159265358979; // Boolean bool isGameOver = false; std::cout << "Integer size: " << sizeof(int) << " bytes" << std::endl; std::cout << "Character: " << rank << " (ASCII: " << (int)rank << ")" << std::endl; std::cout << "Boolean Value: " << isGameOver << " (Displayed as 0)" << std::endl; // Showing precision difference std::cout << std::fixed << std::setprecision(12); std::cout << "Float: " << temperature << std::endl; std::cout << "Double: " << pi << std::endl; return 0; }
C++ allows you to modify data types using signed, unsigned, short, and long.
The Theory of Overflow:
Every data type has a "bucket" size. If you try to store a value larger than the maximum allowed (e.g., storing 130 in a signed char that only goes up to 127), the value will overflow and "wrap around" to the smallest possible value (-128). This is a common source of bugs in financial and scientific software.
Copyright ©2025. All Rights Reserved Emblab THE RAVE INNOVATION