Website For Developers

Data types in C++



In C++ we may need to store different type of data so we need to assign them data type according to their nature and our demand.
There are rich set of built-in data types in C++. Following is the list of basic C ++ data types:

Data types Description
char It is used to store character value and it takes 1 byte.
short It is used to store small integer value and it takes 2 bytes.
int It is used to store integer value and it takes 4 bytes.
long It is used to store large integer value and it takes 4 bytes.
float It is used to store real value and it takes 4 bytes.
double It is used to store large real value and it takes 8 bytes.
long double It is used to store very large real numbers and it takes 12 bytes.
boolean bool use to store two states value like true/false and it takes 1 byte.

Example of data types in C++

#include<iostream>
#include<conio.h>
using namespace std;
int main() {
  cout<< "char size is : "<<sizeof(char)<<endl;
  cout<< "short size is : "<<sizeof(short)<<endl;
  cout<< "int size is : "<<sizeof(int)<<endl;
  cout<< "long size is : "<<sizeof(long)<<endl;
  cout<< "float size is : "<<sizeof(float)<<endl;
  cout<< "double size is : "<<sizeof(double)<<endl;
  cout<< "long double size is : "<<sizeof(long double)<<endl;
  cout<< "bool size is : "<<sizeof(bool)<<endl;
  getch();
  return 0;
}

Output

char size is : 1
short size is : 2
int size is : 4
long size is : 4
float size is : 4
double size is : 8
long double size is : 12
bool size is : 1


Data type also used with the keywords: signed and unsigned
signed data type is restricted user to insert only positive values.
signed and unsigned are used only with character and integers data type.
Keep in mind that short, signed and unsigned not allowed with real values.