C++ Data Types

While writing program in any language, you have to utilize different variables to store different data. Variables are only held memory areas to store values. This implies when you make a variable you save some space in memory.

You may get a kick out of the chance to store data of different information types like character, wide character, whole number, coasting point, twofold skimming point, boolean and so forth. In light of the information kind of a variable, the OS designates memory and chooses what can be put away in the saved memory.

Primitive Built-in Types

C++ offers the software engineer a rich combination of inherent just as user characterized data types. Following table records down seven fundamental C++ data types −

Boolean bool
Character char
Integer int
Floating point float
Double floating point double
Valueless void
Wide character wchar_t

A few of the fundamental types can be adjusted utilizing at least one of these type modifiers −

  • signed
  • unsigned
  • short
  • long

The accompanying table shows the variable type, how much memory it takes to store the incentive in memory, and what is most extreme and least worth which can be put away in such sort of variables.

TypeTypical Bit WidthTypical Range
char1byte-127 to 127 or 0 to 255
unsigned char1byte0 to 255
signed char 1byte -127 to 127
int 4bytes-2147483648 to 2147483647
unsigned int 4bytes 0 to 4294967295
signed int4bytes -2147483648 to 2147483647
short int 2bytes -32768 to 32767
unsigned short int2bytes 0 to 65,535
signed short int 2bytes -32768 to 32767
long int 8bytes-2,147,483,648 to 2,147,483,647
signed long int 8bytes same as long int
unsigned long int8bytes 0 to 4,294,967,295
long long int 8bytes -(2^63) to (2^63)-1
unsigned long long int8bytes 0 to 18,446,744,073,709,551,615
double 8bytes
long double 12bytes
wchar_t 2 or 4 bytes 1 wide character

The size of variable may be not quite the same as those appeared in the above table, contingent upon the compiler and the PC you are utilizing.

Following is the example, which will deliver right size of different information types on your PC.

#include <iostream>
using namespace std;
int main() {
cout <<  "Size of Char : " <<  sizeof(char) <<  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 embeds another line character after each line and << operator is being utilized to pass different qualities out to the screen. We are additionally utilizing sizeof() operator to get size of different data types.

At the point when the above code is compiled and executed, it creates the accompanying outcome which can differ 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

typedef Declarations

You can make another name for a current type utilizing typedef. Following is the straightforward syntax to characterize another type utilizing typedef −

typedef type newname;

For instance, the accompanying tells the compiler that feet is another name for int −

typedef int feet;

Presently, the accompanying revelation is completely lawful and makes a whole number variable called separation −
feet separation;

Enumerated Types

A enumerated type declares a discretionary kind name and a lot of at least zero identifiers that can be utilized as value of the type. Every enumerator is a consistent whose type is the enumeration.

Making a enueration requires the use of the keyoword enum. The general type of a enumeration type is −

enum enum-name { list of names } var-list;

Here, the enum-name is the enumeration's type name. The rundown of names is comma isolated.

For instance, the accompanying code defines a enumeration of colors called color and the variable c of type shading. At last, c is alloted the worth "blue".

enum color { red, green, blue } c;
c = blue;

By default, the value of the first name is 0, the second name has the value 1, and the third has the value 2, and so on. But you can give a name, a specific value by adding an initializer. For example, in the following enumeration, green will have the value 5.

enum color { red, green = 5, blue };

Here, blue will have a value of 6 because each name will be one greater than the one that precedes it.