In programming, when we save information in a computer’s memory, we need to specify what type of information it is, like numbers or letters. This helps the computer understand how to handle that data.
For example, it needs to know if it’s dealing with whole numbers, individual characters, or decimal numbers. Each type has its own range of values it can hold, like how big or small a number can be or which characters it can represent. This way, the computer can manage and process the data correctly.
Data types are variable declarations in C++. The type and size of data associated with variables are determined by a data type.
For example,
int marks = 427;
In the above example, marks
is a variable of type int
. It means that the variable marks
can only store integer values.
Fundamental Data Types in C++
The following table lists the most common data types, their definitions, and their sizes (in bytes):
Data Type | Full Form | Size | Range |
---|---|---|---|
char | Character | 1 Byte | Signed: -128 to 127 |
int | Integer | 4 Bytes | -2,147,483,648 to 2,147,483,647 |
float | Floating Point | 4 Bytes | 1.2E-38 to 3.4E+38 |
bool | Boolean | 1 Byte | true (1) or false (0) |
void | Void (Empty) | nothing" or "no value" | 0 |
You can also download a PDF containing comprehensive details for C++ data types by clicking the download button below.
The Data Types in C++ are divided into;
- Primitive Data Types
- Derived Data Types
- Abstract or User-Defined Data Types
In this tutorial, we will only focus on Primitive Data Types.
Primitive Data Types
These data types are built-in or predefined data types that can be used to declare variables directly by the user. For example, int, char, float, and bool. The following primitive data types are available in C++;
- Integer
- Character
- Floating Point
- Boolean
- Unsigned
- Void
1. Integer Data Type
Integer data types only store integer numbers such as 100, -200, 340, etc. It has the following subtypes:
int
The keyword int represents integer data types. Integers have two bytes of memory (16 bits). A 16-bit number can range from -215 to 215 -1, which means it can hold values in the range of -32768 to 32767.
short int
In C++, the short integer data type is declared with a short int. The short int data type has a maximum size of 2 bytes (16 bits). It may range from -215 to 215 -1, which means it can store values between -32768 and 32767.
long int
The long int data type in C++ is used to store more significant integer values. It has a storage capacity of 4 bytes (32 bits), allowing it to store values in the range of -2147483648 to 2147483647.
2. Character Data Type
In C++, the char
keyword represents the character data type. A character data type is any character that belongs to the ASCII character set. The char
data type has an 8-bit maximum capacity (1 byte). It has two different types: signed char and unsigned char. Both of them consume the same amount of RAM.
For example,
char x;
In the above example, x
is a character-type variable that takes up 1 byte of memory. Character constants are stored in variables of the char
data type. Character constants are indicated by single quotations.
3. Floating Point Data Type
Although integers are only intended to store whole numbers, we sometimes need to store huge numbers or values that include a decimal point. Floating-point data types are data types that are used to store such types of data.
The keyword float
is used to represent the floating-point data type. Real numbers, such as 4.0, 4.99, 3.911, etc., are stored in floating-point variables. Floating point data can be divided into three categories:
float
The float data type is used in C++ to declare floating-point types of variables for storing decimal values. To store values, it requires 4 bytes (32 bits) of RAM.
double
In C++, the keyword double is used to express double-precision floating-point values. The double data type takes up 8 bytes of memory (64 bits). The double data type’s size is a rational integer in the same range as the long float, and it is stored as a floating-point representation with binary mantissa and exponent.
long double
The long double data type is used in C++ to declare long double types of variables, which represent long double-precision floating-point values. It requires a total of 10 bytes (80 bits) of memory in the RAM.
4. Boolean Data Type
The keyword bool
is used to represent the Boolean data type. It can only have one of two values: true 1
or false 0
. It takes up one byte of memory.
5. Unsigned Data Type
The data types described so far are signed data types, which means that one bit is designated for the value’s sign (i.e., +/-). Unsigned numbers are whole numbers that always have positive values from 0 to their most extensive range.
The value is likewise represented by the sign bit, implying that no bit is set aside for holding a sign (+/-). Unsigned numbers can be divided into four categories:
- unsigned char
- unsigned integer
- unsigned short integer
- unsigned long integer
6. Void (Valueless)
The term void
refers to something that has no value. The void data type represents a valueless entity. The void data type is used for functions that do not return a value.
Example: C++ Program To Demonstrate The Concept of Data Types
#include <iostream> using namespace std; int main() { // Integer data type int a = 5; // Floating-point data type float b = 1.53f; // Use 'f' suffix to denote a float literal // Double-precision floating-point data type double c = 3.14156; // Character data type char ch = 'M'; // Boolean data type bool y = false; // Output values cout << "The value of a = " << a << endl; cout << "The value of b = " << b << endl; cout << "The value of c = " << c << endl; cout << "The value of ch = " << ch << endl; cout << "The value of y = " << y << endl; return 0; }
OUTPUT OF THE PROGRAM
The value of a= 5
The value of b= 1.53
The value of c= 3.14156
The value of ch= M
The value of y= 0