Data Types in C++
Data types define the a type of data variable the a variable data can hold. For example, an integer variable can hold integer data, and a character type variable can hold character data.
In C++, data types are categorized into three types:
- Primitive/Built-in data types
- Derived data types
- Abstract/User-defined data types
Built-in Data Types
Built-in data types can be used directly by the user to declare variables. The built-in data types available in C++ are:
Data Type | Keyword |
---|---|
Integer | int |
Character | char |
Boolean | bool |
Floating Point | float |
Double Floating Point | double |
Valueless or Void | void |
Wide Character | wchar_t |
Derived Data Types
Derived Data Types are those that are created by combining primitive or built-in datatypes. Thera are four types of derived-defined data types in C++:
- Function
- Array
- Pointer
- Reference
User-defined Data Types
The User-defined or abstract data types are defined by the user themselves. The different User-defined data types are:
- Class
- Structure
- Union
- Enumeration
- Typedef defined Data Type
Data Type Modifiers in C++
The four modifiers available in C++ are:
- Signed
- Unsigned
- Short
- Long
The table below summarizes the variable type, the size of memory required to hold the value in memory, and the maximum and lowest values stored in such variables. The below values may vary from one compiler to another. GCC 32 bit was considered in the following values.
Data Type | Size (in bytes) | Range |
---|---|---|
char | 1 | -127 to 127 or 0 to 255 |
unsigned char | 1 | 0 to 255 |
signed char | 1 | -127 to 127 |
short int | 2 | -32,768 to 32,767 |
unsigned short int | 2 | 0 to 65,535 |
signed short int | 2 | -32,768 to 32,767 |
int | 4 | -2,147,483,648 to 2,147,483,647 |
unsigned int | 4 | 0 to 4,294,967,295 |
signed int | 4 | -2,147,483,648 to 2,147,483,647 |
long int | 8 | 9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
signed long int | 8 | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
unsigned long int | 8 | 0 to 18,446,744,073,709,551,615 |
long long int | 8 | -(2^63) to (2^63)-1 |
unsigned long long int | 8 | 0 to 18,446,744,073,709,551,615 |
float | 4 | |
double | 8 | |
long double | 12 | |
wchar_t | 2 or 4 | 1 wide character |
The below program will display the correct size of various data types:
#include<bits/stdc++.h>
using namespace std;
int main() {
cout << "Size of following Data Types in bytes: \n";
cout << "int : " << sizeof(int) << endl;
cout << "unsigned int : " << sizeof(unsigned int) << endl;
cout << "short int : " << sizeof(short int) << endl;
cout << "long int : " << sizeof(long int) << endl;
cout << "unsigned short int : " << sizeof(unsigned short int) << endl;
cout << "unsigned long int : " << sizeof(unsigned long int) << endl;
cout << "long long int : " << sizeof(long long int) << endl;
cout << "unsigned long long int : " << sizeof(unsigned long long int) << endl;
cout << "signed char : " << sizeof(signed char) << endl;
cout << "unsigned char : " << sizeof(unsigned char) << endl;
cout << "wchar_t : " << sizeof(wchar_t) << endl;
cout << "float : " << sizeof(float) << endl;
cout << "double : " << sizeof(double) << endl;
return 0;
}