Data Types in C++

How Data Types work in C++

While writing code in any language we need variables to store different data and information (variables). These information can be in different format like –

  • Numbers/Integers/Decimals
  • Characters
  • Boolean
  • Characteristic object etc
Data Types in C++

Data types classify and strictly define the type and allowed use of a defined variable. There are various types of data types like – int, float, char etc.

Each binds the variable with only specific type of input and allocates a pre-defined memory space that is defined for the data type in the memory.

Read more –

Different Types of Data Types in C++

Note – A lot of wrong information is given on geeks4geeks.

The following are the different types of data types available in C++ :

  • Built in (Also known as Primitive Data Types)
  • User Defined (Also known as Abstract Data Types)
  • Derived

Built in Data Types (Primitive Data Types)

These data types are built in C++ and can be directly used by the user by using specific keyword before the variable

  • Example – int age = 12;
Data Types in C++Keyword
Booleanbool
Characterchar
Integerint
Floating Pointfloat
Double Floating Pointdouble
No Valuevoid
Wide characterwchar_t

Modifiers

C++ allows data types to be preceded with modifiers like signed, unsigned, long, short etc. These modifiers change/alter the behaviour of data types and give user more flexibility and data handling security in many cases. Some examples of data modifiers are –

  • signed
  • unsigned
  • short
  • long
Data TypeSizeRange
bool1 byteTrue/False
char1 byte-128 to 127 or 0 to 255
signed char1 byte-128 to 127
unsigned char1 byte0 to 255
int4 bytes-2147483648 to 2147483647
signed int4 bytes-2147483648 to 2147483647
unsigned int4 bytes0 to 4294967295
short int2 bytes-32,768 to 32,767
signed short int2 bytes-32,768 to 32,767
unsigned short int2 bytes0 to 65,535
long int4 bytes-2,147,483,648 to 2,147,483,647
signed long int4 bytes-2,147,483,648 to 2,147,483,647
unsigned long int4 bytes0 to 4,294,967,295
float4 bytes1.17549e-38 to 3.40282e+38
Double Floating Point8 bytes2.22507e-308 to 1.79769e+308
No ValueNot validNot valid
Wide character2 or 41 wide character

The following code will give you output of the size of each data type –

// 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; 
}

The output of the code depending on the default sizes of data types and modifiers in your compiler will be –

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