C++ offer the programmer a rich assortment of built-in as well as user defined data types. Following table lists down seven basic C++ data types.
Type | Keyword |
---|---|
Boolean | bool |
Character | char |
Integer | int |
Floating point | float |
Double floating point | double |
Wide character | wchar_t |
Several of the basic types can be modified using one or more of these type modifier:
- signed
- unsigned
- short
- 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 vaue which can be stored in such type of variables.
Type | Typical Bit Width | Typical Range |
---|---|---|
unsigned char | 1byte | 0 to 255 |
(signed) char | 1byte | -127 to 127 |
unsigned int | 4bytes | 0 to 4294967295 |
(signed) int | 4bytes | -2147483648 to 2147483647 |
unsigned short int | 2bytes | 0 to 65535 |
(signed) short int | 2bytes | -32768 to 32767 |
unsigned long int | 8bytes | 0 to 4294967295 |
(signed) long int | 8bytes | -2147483647 to 2147483647 |
float | 4bytes | 1.18e-38 to 3.40e38 (7 dígitos) |
double | 8bytes | 2.23e-308 to 1.79e308 (15 dígitos) |
long double | 16bytes | 3.37e-4932 to 1.18e4932 (18 dígitos) |
wchar_t | 4 bytes | 1 wide character |
The sizes of variables might be different from those shown in the above table, depending on the compiler and the computer you are using.
#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;
}