Data Types in CPP (C Plus Plus)
Data Types in C++
What are Data Types?
All variables/arrays etc use data-type during declaration to restrict the type of data to be stored. Every data type occupies a different amount of memory.
Commonly Used C++ Data Types
Variable Type | Variable Size in Byte | Range |
int | 4 bytes | -2147483648 to 2147483647 |
float | 4 bytes | +/- 3.4e +/- 38 (~7 digits) |
double | 8 bytes | +/- 1.7e +/- 308 (~15 digits) |
char | 1 byte | -127 to 127 or 0 to 255 |
Rarely Used C++ Data Types
Variable Type | Variable Size in Byte | Range |
unsigned int | 4 bytes | 0 to 4294967295 |
signed int | 4 bytes | -2147483648 to 2147483647 |
short int | 2 bytes | -32768 to 32767 |
unsigned short int | 2 bytes | 0 to 65,535 |
signed short int | 2 bytes | -32768 to 32767 |
long int | 4 bytes | -2,147,483,648 to 2,147,483,647 |
signed long int | 4 bytes | same as long int |
unsigned long int | 4 bytes | 0 to 4,294,967,295 |
long double | 8 bytes | +/- 1.7e +/- 308 (~15 digits) |
wchar_t | 2 or 4 bytes | 1 wide character |
unsigned char | 1 byte | 0 to 255 |
signed char | 1 byte | -127 to 127 |
The size of the variables might be different from those shown in the above table, depending on the PC and the PC you are using. Following is the example, which will produce the correct size of various data types on your system. we can use sizeof(data type) for finding size in coding.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
#include <iostream> // header using namespace std; // including namespace std int main()// main { // output int size cout << "Size of int = " << sizeof(int) << endl; // output char size cout << "Size of char = " << sizeof(char
) << endl; // output float size cout << "Size of float = " << sizeof(float) << endl; // output double size cout << "Size of double = " << sizeof(double) << endl; return 0; } |
Output
Size of int = 4
Size of char = 1
Size of float = 4
Size of double = 8