Our today’s topic in learning C++ is data types. Data types are used in the C++ program to determine the type of data which needs to be stored in a variable. It also decides the size of the memory location is allocated for storing the data. Data types in C++ are classified into 3 different types:
- Built-in/primitive data type
- Integral type
- Integer(int)
- Character(char)
- Floating point
- Floating point(float)
- Double floating point(double)
- Void
- Boolean
- Wide character
- Integral type
- User-defined data type
- Struct
- Union
- Class
- enumeration
- Derived data type
- Array
- Reference
- Function
- Pointer
So, the pictorial representation of the classification would be like this:

Starting with Primitive data type/ built-in data types, these are the data types that can be directly used by the user and these are not made by a combination of other data types. These are also called atomic data types as said above they are not composed of other data types.
Primitive data types are of 5 categories:
- Integral types: Integral data types consists of integers(used to store whole numbers) which are denoted by int and character type(used to store characters) which is denoted by char.
- Example:
int a= 15; char c='a';
- Floating types: These consist of floating-point data type(used to store decimal numbers and fractional numbers with single precision) denoted by the float and double floating-point data type(used to store float data with double precision) denoted by double.
Example:
float length= 12.54; double area= 32.34562;
- Void: It is used to specify the return type of function and it is also used indicate an empty argument list to a function.
- Boolean: These are used in conditional statements and loops as Booleans can only have one of the 2 values, it is either true or false.
Example:
bool value='true';
- Wide Character: These are used to store character type data which require more space than the usual character type as the wide character type is allocated with 2 bytes of memory instead of 1 byte. It is denoted by wchar_t.
Now, moving to derived data types, these are the data types that are derived from primitive data types. These are of 4 types:
- Array: An Array is a collection of homogeneous elements.
Example:
char arr[5]="shiva";
- Pointer: It points to a specific data location or address.
Example:
int *mp; mp=&y; *mp=3;
- Function: Functions are used to perform different tasks like to add values, to multiply, etc. The function starts with a function name followed by 2 brackets then the required statements are written and this function is called by an object.
- Reference: &
The basic primitive data types can be modified by using different Data type modifiers to be used for different purposes. There are 4 types of data type modifiers:
- Signed
- Unsigned
- Long
- Short
So, the following data shows different data types, storage space required for them and their range.
DATA TYPE | SPACE REQUIRED | SPECIFIC RANGE |
Int | 4 bytes | -2147483648 to 2147483647 |
Signed int | 4 bytes | -2147483648 to 2147483647 |
Unsigned int | 4 bytes | 0 to 4294967295 |
Short int | 2 bytes | -32768 to 32767 |
Long int | 4 bytes | -2147483648 to 2147483647 |
Unsigned short int | 2 bytes | 0 to 65535 |
Signed short int | 2 bytes | -32768 to 32767 |
Unsigned long int | 4 bytes | 0 to 4294967295 |
Signed long int | 4 bytes | -2147483648 to 2147483647 |
Char | 1 byte | -128 to 127 or 0 to 255 |
Unsigned char | 1 byte | 0 to 255 |
Signed char | 1byte | -128 to 127 |
Double | 8 bytes | +/- 1.7e +/- 308 (~15 digits) |
Long double | 8 bytes | +/- 1.7e +/- 308 (~15 digits) |
Float | 4 bytes | +/- 3.4e +/- 38 (~7 digits) |
wchar_t | 2 or 4 bytes | 1 wide character |
Bool | 1 byte | True or false |
Example program to show the size of each data type:
#include <iostream> using namespace std; int main() { cout << "Size of int is " << sizeof(int) << endl; cout << "Size of short int is " << sizeof(short int) << endl; cout << "Size of long int is " << sizeof(long int) << endl; cout << "Size of char is " << sizeof(char) << endl; cout << "Size of double is " << sizeof(double) << endl; cout << "Size of float is " << sizeof(float) << endl; cout << "Size of wchar_t is " << sizeof(wchar_t) << endl; return 0; }
Below screenshot shows the size of each data type used by the compiler:
So, our last category of data types is user-defined data types, these are data types which are defined by the users for suitable purposes. These are of 4 types:
- Struct
- Union
- Enumeration
- Class
We didn’t discuss much the derived and user-defined data types to the root as they separate chapters in C++ regarding the same. To keep the article short and easy to grasp we have to end it here only. Thank You.
Please write comments or WhatsApp if you find anything incorrect, or you want to share more information about the topic discussed above.