Data types in C++

What are Data types in C++?

To write a program in any language, you need to use various variables to store various information of day to day life of as per the program. Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory. This space is allotted to the variable name which we have given while declaring a variable.

You may like to store information of various data types like character, wide character, integer, floating point, double floating point, boolean etc. Based on the data type of a variable, the operating system allocates memory and decides what can be stored in the reserved memory so that the required function can be performed.

Primitive Built-in Types

C++ offers the programmer a rich assortment of built-in as well as user defined data types. Following table lists down seven basic C++ data types

TypeKeyword
Booleanbool
Characterchar
Integerint
Floating pointfloat
Double floating pointdouble
Valuelessvoid
Wide characterwchar_t

Several of the basic types can be modified using one or more of these type modifiers −

Signed, unsigned, short and long

The following table shows the variable type, how much memory it takes to store the value in memory, and what is maximum and minimum value which can be stored in such type of variables.

TypeTypical Bit WidthTypical Range
char1byte-127 to 127 or 0 to 255
unsigned char1byte0 to 255
signed char1byte-127 to 127
int4bytes-2147483648 to 2147483647
unsigned int4bytes0 to 4294967295
signed int4bytes-2147483648 to 2147483647
short int2bytes-32768 to 32767
unsigned short int2bytes0 to 65,535
signed short int2bytes-32768 to 32767
long int8bytes-2,147,483,648 to 2,147,483,647
signed long int8bytessame as long int
unsigned long int8bytes0 to 18,446,744,073,709,551,615
long long int8bytes-(2^63) to (2^63)-1
unsigned long long int8bytes0 to 18,446,744,073,709,551,615
float4bytes
double8bytes
long double12bytes
wchar_t2 or 4 bytes1 wide character

The size of variables might be different from those shown in the above table, depending on the compiler and the operating system installed on computer you are using i.e whether it is 32 bit or 64 bit operating system.

Following is the example, which will produce correct size of various data types on your computer.

#include <iostream>
using namespace std;
int main() {
   cout << "Size of char : " << sizeof(char) << endl;
   cout << "Size of int : " << sizeof(int) << endl;
   cout << "Size of short int : " << sizeof(short int) << endl;
   cout << "Size of long int : " << sizeof(long int) << endl;
   cout << "Size of float : " << sizeof(float) << endl;
   cout << "Size of double : " << sizeof(double) << endl;
   cout << "Size of wchar_t : " << sizeof(wchar_t) << endl;
   return 0;
}

This example uses endl, which inserts a new-line character after every line and << operator is being used to pass multiple values out to the screen. We are also using sizeof() operator which is used to get the size of various data types.

When the above code is compiled and executed, it produces the following result which can vary from machine to machine −

Size of char : 1

Size of int : 4

Size of short int : 2

Size of long int : 4

Size of float : 4

Size of double : 8

Size of wchar_t : 4