In C++ programming language, data type specifies the type of data a variable can store and how much memory is required to store this data. The data type of a variable also defines the format in which a data of particular type should be stored.
C++ Primitive Data Types
There are seven basic data types in C++ programming language.- Boolean
- Character
- Integer
- Floating point
- Double floating point
- Void
- Wide character
C++ Data Type Modifiers
C++ data type modifiers specifies the amount of memory space to be allocated for a variable. Modifiers are prefixed with basic data types to modify the amount of memory allocated for a variable.
For Example in a 16 bit system, the size of int data type is 2 bytes. If we add long prefix in integer variable declaration(long int), it's size becomes 8 bytes.
- long
- short
- signed
- unsigned
The size of basic data types is system dependent. Size of an integer data type in a 32 bit computer is 4 bytes whereas size of integer data type in 16 bit computer is 2 bytes.
We can use sizeof operator to know the exact size of any data type.
The following table, shows the different basic data types, their size and value range in a 32 bit operating system.
Data Types | Memory Occupied in bytes | Min-Max Range |
---|---|---|
char | 1 bytes | -128 to 127 or 0 to 255 |
unsigned char | 1 bytes | 0 to 255 |
signed char | 1 bytes | -128 to 127 |
wchar_t | 2-4 bytes | 1 wide character |
short int | 2 bytes | -32768 to 32767 |
unsigned short int | 2 bytes | 0 to 65,535 |
signed short int | 2 bytes | -32768 to 32767 |
int | 4 bytes | -2147483648 to 2147483647 |
unsigned int | 4 bytes | 0 to 4294967295 |
signed int | 4 bytes | -2147483648 to 2147483647 |
long int | 8 byte | -2,147,483,648 to 2,147,483,647 |
signed long int | 8 byte | -2,147,483,648 to 2,147,483,647 |
unsigned long int | 8 byte | 0 to 4,294,967,295 |
float | 4 bytes | +/- 3.4e +/- 38 (~7 digits) |
double | 8 bytes | +/- 1.7e +/- 308 (~15 digits) |
long double | 8 bytes | +/- 1.7e +/- 308 (~15 digits) |
C++ Void Data Type
The void data type in C++ means no data. We cannot define variables of void data types.
C++ Boolean Data Type
The boolean data type can only represent one of two states, true or false. Keyword bool is used to declare variables of boolean type.
For Example:bool isAvailable = true;
C++ Integer Data Type
Integer data type in C++ is used to store a value of numeric type. Memory size of a variable of integer data type is dependent on Operating System, For example size of an integer data type in a 32 bit computer is 4 bytes whereas size of integer data type in 16 bit computer is 2 bytes.
Keyword int is used to declare variables of type integer. Range of integer(int) data type in 16 Bit system is -32,768 to 32,767.
int i = 2015;
Above statement declares a variable 'i' of integer data type and stores 2015 in it's memory location.
C++ Character Data Type
Character data type is used to store a character. A variable of character data type allocated only one byte of memory and can store only one character. Keyword char is used to declare variables of type character. Range of character(char) data type is -128 to 127.
For Example:char c = 'A';
Above statement declares a variable 'c' of character data type and stores character 'A' in it's memory location.
C++ Floating Point Data Type
Floating point data type in C++ can be sub-divided into two types on the basis of precision and size.
- float : 4 bytes with six digits of precision.
- double : 8 bytes with ten digits of precisionfloat data type.
Floating point data type in C++ is used to store a value of decimal values. Memory size of a variable of floating point data type is dependent on Operating System, For example size of an floating point data type in a 16 bit computer is 4 bytes. Keyword float is used to declare variables of floating point type. Floating point data type provides up-to 6 digits of precision.
For Example:float fl = 11.243567f;
Above statement declares a variable 'fl' of float data type and stores 11.243567f in it's memory location.
double data typeFloating point data type similar to float data type except it provides up-to ten digit of precision and occupies eight bytes of memory.
For Example:double d = 11676.2435676542;
Above statement declares a variable 'd' of double data type and stores 11676.2435676542 in it's memory location.
C++ Program to Show Size of Basic Data Types
#include <iostream> using namespace std; int main(){ /* * Size of Data type is dependent on Operating System * I am running this program in a 32 bit OS */ cout << "Size for char : " << sizeof(char) << endl; cout << "Size for int : " << sizeof(int) << endl; cout << "Size for float : " << sizeof(float) << endl; cout << "Size for double : " << sizeof(double) << endl; return 0; }
Output
Size for char : 1 Size for int : 4 Size for float : 4 Size for double : 8As explained above, the size of a variable depends on the programming environment. I ran above program in a 32 bit computer and produced above output.