C++ provides a set of fundamental data types, also known as primitive data types, which are built directly into the language. These types are essential for storing and manipulating data in a C++ program. The key primitive data types in C++ include:
Character types
Integer types (both signed and unsigned)
Floating point types
Boolean type
It is important to note that, unlike some other programming languages, the size and precision of C++ primitive data types depend on the platform and compiler being used. This means that as a C++ programmer, you need to be aware of the specific storage allocations for these types on your system.
Binary Representation and Storage
Computers store information using a binary system, consisting of ones and zeros. The number of bits allocated to a type determines the number of unique values it can represent. The formula used to determine the number of values a data type can hold is:
2^number_of_bits
For example:
8 bits can store 256 distinct values
16 bits can store 65,536 distinct values
32 bits can store over 4 billion distinct values
Character Data Type
The char
data type in C++ is used to store single characters, such as 'A' or 'X'. Typically, a char
is implemented using 8 bits, allowing it to represent up to 256 different characters. This is sufficient for the Latin alphabet but not for languages with thousands of characters.
To support these languages, C++ provides a wchar_t
(wide character type), which can be larger than 8 bits. Unicode is commonly used to handle multiple character sets across different languages.
Example Usage:
char middle_initial = 'J';
std::cout << "My middle initial is " << middle_initial << std::endl;
Integer Data Types
Integers store whole numbers and can be either signed (allowing negative values) or unsigned (only positive values). C++ provides several variations of the integer data type:
Data Type | Typical Size |
short | 16 bits |
int | 32 bits |
long | 32 or 64 bits |
long long | 64 bits |
By default, integers are signed. If an unsigned integer is required, the unsigned
keyword must be explicitly used. Signed and unsigned refer to whether an integer type can store negative numbers or only non-negative values. A signed integer can store both positive and negative numbers and an unsigned integer can only store non-negative values (0 and positive numbers).
Example Usage:
unsigned short exam_score = 55;
std::cout << "Exam score: " << exam_score << std::endl;
When choosing an integer type, consider the range of values your variable needs to store. For example, if you need to store a large number, such as the population of a country, using a long long
might be appropriate:
long long world_population = 7600000000;
std::cout << "World population: " << world_population << std::endl;
Overflow Example
If a variable exceeds its storage limit, an overflow occurs. For instance:
short value1 = 30000;
short value2 = 1000;
short product = value1 * value2;
std::cout << "Product: " << product << std::endl; // Product: -15488
Since the product exceeds the short
data type’s range, the result will be incorrect. The problem occurs because short
is a 16-bit data type, which means it can only store values in the range of -32,768 to 32,767. Since 30,000,000
exceeds the limit, the extra bits overflow into the negative range.
Floating Point Types
Floating point types store real numbers (numbers with a decimal point). These include:
Data Type | Typical Precision |
float | 7 decimal digits |
double | 15 decimal digits |
long double | More than 15 digits |
Since computers have finite storage, floating point numbers are approximations. For example, the value of pi cannot be stored precisely.
Example Usage:
float pi_float = 3.141592653589793;
std::cout << pi_float << std::endl; // 3.14159
Floating-point numbers in C++ (float
, double
, long double
) are stored in scientific notation using a mantissa and exponent.
For example, a floating-point number is stored in the form:
Since floating-point numbers use binary representation, some decimal numbers cannot be exactly represented and since float
cannot store all decimal places, the value is rounded to 3.14159, which is not exact.
Boolean Data Type
The Boolean data type represents true
or false
. In C++, false
is stored as 0
, and true
is any nonzero value.
Example Usage:
bool game_over = false;
std::cout << "Game over: " << game_over << std::endl; // Game over: 0
game_over = true;
std::cout << "Game over: " << game_over << std::endl; // Game over: 1
This will output 0
for the false
and 1 for true
is represented as 0
and 1
in C++.
Discover hands-on programming tutorials and resources! Visit my website: Fullstack Dev