Data types form the foundation of data representation in C++. Every variable must be associated with a data type that defines the kind of value it can store and how that value is handled by the compiler.
- Data types determine the memory allocation, range of values, and operations supported by a variable.
- Understanding different data types is essential for writing correct, efficient, and maintainable C++ programs.
Classification of Data Types in C++
Data types in C++ can be broadly classified into three categories:

Integer Data Type (int)
The int data type is used to store whole numbers.
#include <iostream>
using namespace std;
int main() {
// Creating a variable to store integer
int var = 10;
cout << var;
return 0;
}
Output
10
Explanation: The variable var stores the integer value 10. The keyword int specifies that the variable can hold integer values.
Note: The C++ standard does not specify the exact memory size of data types (int, float, etc.). Their size may vary depending on the compiler and system architecture. The sizes mentioned in this article are common for many modern 64-bit systems but may differ on other platforms.
Let us explore how primitive data types are used in C++.
Character Data Type (char)
The char data type stores a single character enclosed in single quotes. Typically occupies 1 byte and stores the ASCII value of the character internally.
#include <iostream>
using namespace std;
int main()
{
// Character variable
char c = 'A';
cout << c;
return 0;
}
Output
A
Boolean Data Type (bool)
The boolean data type is used to store logical values: true(1) or false(0). The keyword used to define a boolean variable is bool. Its typical size is 1 byte.
#include <iostream>
using namespace std;
int main()
{
// Creating a boolean variable
bool isTrue = true;
cout << isTrue;
return 0;
}
Output
1
Floating Point Data Type (float)
Float is used to store decimal numbers. It is declared using the float keyword, typically occupies 4 bytes, and can store values approximately from 1.2e-38 to 3.4e+38.
float f = 36.5f;
Double Data Type (double)
Double (double) is used to store decimal numbers with higher precision than float. It typically occupies 8 bytes and can store values approximately from 1.7e-308 to 1.7e+308.
double pi = 3.1415926535;
Void Data Type (void)
The void data type represents the absence of value. We cannot create a variable of void type. It is used for pointer and functions that do not return any value using the keyword void.
void display() {
cout << "Hello";
}
Derived Data Types in C++
Derived data types are formed using fundamental data types.
Array
An array stores multiple values of the same type in contiguous memory locations.
int arr[5] = {1, 2, 3, 4, 5};
Pointer
A pointer stores the memory address of another variable.
int x = 10;
int* ptr = &x;
Reference
A reference acts as an alias for an existing variable.
int x = 10;
int & ref = x;
Function Type
Functions also represent a derived type because they have a specific return type and parameter types.
int add(int a, int b) {
return a + b;
}
User Defined Data Types in C++
User-defined data types allow programmers to create their own data structures.
Structure (struct)
A structure groups related variables under one name.
struct Student {
string name;
int age;
};
Class
A class combines data and functions into a single unit.
class Student {
public:
string name;
int age;
};
Union
A union allows multiple members to share the same memory location.
union Data {
int i;
float f;
};
Type Aliases
Type aliases provide alternative names for existing types.
typedef unsigned long ulong;
using ll = long long;
Size of Data Types in C++
The exact size of a data type depends on the compiler and platform.
| Data Type | Typical Size |
|---|---|
| char | 1 byte |
| bool | 1 byte |
| int | 4 bytes |
| float | 4 bytes |
| double | 8 bytes |
The sizeof operator can be used to determine the actual size on a system.
cout << sizeof(int);
Data Type Modifiers
C++ provides modifiers that alter the range or representation of existing data types.
- short
- long
- signed
- unsigned
short int a;
long int b;
unsigned int c;
long long int d;