Data types in C++ programming

We have to work with different data types in C++ at different times. When we declare a variable, that means the compiler will reserve a space in the memory to store a data inside that variable. As we work with different data types and for this there are several data types in C++ language.

The 4 main types of data types in C++ are as follows.

Data types category Data types
Basic data type
int, long int, char, float, double
Derived data type
array, pointer, structure, union
Void data type
Enumeration data type

Here in this article we will learn about data types and their uses. So, let’s go ahead.

Basic data types in C++

Basic data types are built-in data types and are most uses data types. C++ supports both signed and unsigned data types. The size of data types depends on your systems configuration. Let’s see the list of some most uses basic data types with their memory size and limits of data they can hold.

Data type Keyword Size (Byte) Range Placeholder
-127 to 128
unsigned character
unsigned char
0 to 255
−2147483648 to 2147483647
short integer
short int
−32,768 to 32,767
long integer
long int
-2,147,483,648 to 2,147,483,647
%l or %ld or %li
long long integer
long long int
-2^63 to 2^63-1
%lli or %lld
1.2E-38 to 3.4E+38
2.3E-308 to 1.7E+308

These are some common data types. There are some other basic data types too which we will learn at the time of their uses.

int data type:

int data types can hold integer type of data. Size of int data type is 4 byte and they can hold numbers from -2147483648 to 2147483647. We can not store any floating point number inside the integer data type.

int age;

int num = 30;

char data type:

char data type can hold any character inside it. Memory size of character data type is 1 byte and they can hold character form ASCII value of -128 to 127.

char myChar;

char ch = 'b';

wchar_t data type:

wchar_t is also used to hold character but it’s memory size is 2 bytes and for this it can hold the character for which we need more memory space.

wchar_t hibruChar

wchar_t hibru = L'ם';

float data types:

float is used to store fraction numbers. float is sufficient for 7 decimal digits and should not use where we need to use fraction number more than 7 digits after radix point.

float pressure;

float atm = 1.01325;

double data types:

double is also used for storing fraction numbers and they can store number with 15 decimal points. For this we should use double instead of float to store data where we need to calculate anything more scientifically.

double sciNum;

double avogadrosNum = 6023E20

bool data type:

bool data types can hold Boolean value which may be true or false.

bool decision;

bool decision = true;

Derived data types in C++

Derived data type is also used in C++ language. Array, structure, pointer, union are some of derived data types which we will learn separately on different chapters.

void data types in C++

We will learn about void data type at our function chapter. void means empty or absence of data. Actually we can not declare void data type.

Enumerated data types in C++

The enum keyword is used to create an enumerated data type. syntax of enum data type is as follows;

enum enum_name { list } var;

We have to separate all the list by comma and by default the value of each list will start from zero. See the following example.

enum day {sat, sun, mon, tue, wed, thu, fri} weekday;
weekday = sat;

Here the value of weekday which is sat. That means the value of weekday will be zero. And then the value of sun = 1, mon = 2 and so on. But we can also use another value instead.

enum day {sat, sun, mon = 5, tue, wed, thu = 12, fri} weekday;

In this example the value of 

sat = 0;

sun = 1;

mon = 5;

tue = 6;

wed = 7;

thu = 12;

fri = 13;

Don’t worry, we will discuss about enumeration later in a separate chapter.