C++ Data Types - GeeksforGeeks

28 Jan, 2022
All variables use data-type during declaration to restrict the type of data to be stored. Therefore, we can say that data types are used to tell the variables the type of data it can store. Whenever a variable is defined in C++, the compiler allocates some memory for that variable based on the data-type with which it is declared. Every data type requires a different amount of memory. 
Data types in C++ is mainly divided into three types:  
Primitive Data Types: These data types are built-in or predefined data types and can be used directly by the user to declare variables. example: int, char , float, bool etc. Primitive data types available in C++ are: IntegerCharacterBooleanFloating PointDouble Floating PointValueless or VoidWide CharacterDerived Data Types: The data-types that are derived from the primitive or built-in datatypes are referred to as Derived Data Types. These can be of four types namely: FunctionArrayPointerReferenceAbstract or User-Defined Data Types: These data types are defined by user itself. Like, defining a class in C++ or a structure. C++ provides the following user-defined datatypes: ClassStructureUnionEnumerationTypedef defined DataType
Primitive Data Types: These data types are built-in or predefined data types and can be used directly by the user to declare variables. example: int, char , float, bool etc. Primitive data types available in C++ are: IntegerCharacterBooleanFloating PointDouble Floating PointValueless or VoidWide Character
Integer
Character
Boolean
Floating Point
Double Floating Point
Valueless or Void
Wide Character
Derived Data Types: The data-types that are derived from the primitive or built-in datatypes are referred to as Derived Data Types. These can be of four types namely: FunctionArrayPointerReference
Function
Array
Pointer
Reference
Abstract or User-Defined Data Types: These data types are defined by user itself. Like, defining a class in C++ or a structure. C++ provides the following user-defined datatypes: ClassStructureUnionEnumerationTypedef defined DataType
Class
Structure
Union
Enumeration
Typedef defined DataType
This article discusses primitive data types available in C++.  
Integer: Keyword used for integer data types is int. Integers typically requires 4 bytes of memory space and ranges from -2147483648 to 2147483647.  
Character: Character data type is used for storing characters. Keyword used for character data type is char. Characters typically requires 1 byte of memory space and ranges from -128 to 127 or 0 to 255.  
Boolean: Boolean data type is used for storing boolean or logical values. A boolean variable can store either true or false. Keyword used for boolean data type is bool.  
Floating Point: Floating Point data type is used for storing single precision floating point values or decimal values. Keyword used for floating point data type is float. Float variables typically requires 4 byte of memory space.  
Double Floating Point: Double Floating Point data type is used for storing double precision floating point values or decimal values. Keyword used for double floating point data type is double. Double variables typically requires 8 byte of memory space.  
void: Void means without any value. void datatype represents a valueless entity. Void data type is used for those function which does not returns a value.  
Wide Character: Wide character data type is also a character data type but this data type has size greater than the normal 8-bit datatype. Represented by wchar_t. It is generally 2 or 4 bytes long.  
 
Datatype Modifiers
As the name implies, datatype modifiers are used with the built-in data types to modify the length of data that a particular data type can hold.  
Data type modifiers available in C++ are:  
Signed
Unsigned
Short
Long
Below table summarizes the modified size and range of built-in datatypes when combined with the type modifiers: 
Note : Above values may vary from compiler to compiler. In the above example, we have considered GCC 32 bit.We can display the size of all the data types by using the sizeof() operator and passing the keyword of the datatype as argument to this function as shown below:  
CPP
// C++ program to sizes of data types#include<iostream>using namespace std; int main(){    cout << "Size of char : " << sizeof(char)      << " byte" << endl;    cout << "Size of int : " << sizeof(int)      << " bytes" << endl;    cout << "Size of short int : " << sizeof(short int)      << " bytes" << endl;    cout << "Size of long int : " << sizeof(long int)       << " bytes" << endl;    cout << "Size of signed long int : " << sizeof(signed long int)       << " bytes" << endl;    cout << "Size of unsigned long int : " << sizeof(unsigned long int)       << " bytes" << endl;    cout << "Size of float : " << sizeof(float)       << " bytes" <<endl;    cout << "Size of double : " << sizeof(double)       << " bytes" << endl;    cout << "Size of wchar_t : " << sizeof(wchar_t)       << " bytes" <<endl;         return 0;}
Output:  
Size of char : 1 byte
Size of int : 4 bytes
Size of short int : 2 bytes
Size of long int : 8 bytes
Size of signed long int : 8 bytes
Size of unsigned long int : 8 bytes
Size of float : 4 bytes
Size of double : 8 bytes
Size of wchar_t : 4 bytes
This article is contributed by Harsh Agarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 
Naman-Bhalla
Abhi rex
adityamalik8087
glitch_09
whysodarkbro
tusharmalhotra494
tonima2003
C Basics
CBSE - Class 11
CPP-Basics
Data Type
school-programming
C++
Mathematical
School Programming
Strings
Strings
Mathematical
Data Type
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Vector in C++ STL
Initialize a vector in C++ (6 different ways)
std::sort() in C++ STL
Bitwise Operators in C/C++
Socket Programming in C/C++
Program for Fibonacci numbers
Write a program to print all permutations of a given string
Coin Change | DP-7
Merge two sorted arrays
Program to find GCD or HCF of two numbers