×
>
<

Aptitude

Data Types in C++

How Data Types Work in C++

Here, on this page, we will discuss the different data types in C++. While writing code in any language we need variables to store different data and information. This information can be in different formats like –

  • Number/Integers/Decimals
  • Characters
  • Boolean
  • Characteristic Object etc.

What are data types?

Data types are used by variables to tell what kind of data it can store. Example – character/integer/decimal etc.

There are three types of data types in C++ –

  • Primary
  • Derived
  • User-defind

Data Types Modifiers

These are used in conjunction with primitive(built-in) data types to modify the length of data that a particular data type can hold these are –

  • Unsigned
  • Signed
  • Short
  • Long

Initialization of Primary Data types

Below we have given a list of all primitive data types and their usage –

Data Types in C++Short NameStorage Type
Boolean bool Boolean or logical values like True/False
Character char Characters values
Integer int Integer values
Floating Point float Single precision Decimal Values
Double Floating Point double Double precision Decimal Values
void void Valueless entity / Null type
Wide character wchar_t Character data type with larger values

Below we have given data types and their sizes, limits and types with different modifiers.

Data Types Size and Range
Data Type Size Range
bool 1 byte True/False
char 1 byte -128 to 127 or 0 to 255
signed char 1 byte -128 to 127
unsigned char 1 byte 0 to 255
short int 2 bytes -32,768 to 32,767
signed short int 2 bytes -32,768 to 32,767
unsigned short int 2 bytes 0 to 65,535
int 2 or 4 bytes -32768 to +32768 or -2,147,483,648 to +2,147,483,648
signed int 2 or 4 bytes -32768 to +32768 or -2,147,483,648 to +2,147,483,648
unsigned int 2 or 4 bytes 0 to +65,535 or 0 to +4,294,967,295
long int 4 or 8 bytes -2,147,483,648 to +2,147,483,647 or -9,223,372,036,854,775,808 to +9,223,372,036,854,775,807
signed long int 4 or 8 bytes 0 to 4,294,967,295 or 0 to 18,446,744,073,709,551,615
unsigned long int 4 or 8 bytes -2,147,483,648 to +2,147,483,647 or -9,223,372,036,854,775,808 to +9,223,372,036,854,775,807
float 4 bytes 1.17549e-38 to 3.40282e+38
Double Floating Point 8 bytes 2.22507e-308 to 1.79769e+308
Long Double Floating Point 16 bytes -3.3621e-4932 to +3.3621e-4932
Wide character 2 or 4 1 wide character
C++ Code to Print the size of datatypes :

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

  
#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
  
Understanding Each Datatype

Integer (int)

  • Uses to store integer values like : -200, 150, 6812 etc
  • Usual Range – it can store values from -2147483648 to 2147483647
  • Usual Size – 4 bytes(some older compilers may support 2 bytes)

  
int age = 25;
  

Float and Double

Float and double are used to store floating-point numbers (decimals and exponentials)

  • The size of float is 4 bytes
  • The size of the double is 8 bytes.
  • Double has two times the precision of float.

  
Datatype                               Range                            Macro
float                       1.17549e-38 to 3.40282e+38                  FLT_MIN 
float(negative)            -1.17549e-38 to -3.40282e+38                -FLT_MIN
double                      2.22507e-308 to  1.79769e+308               DBL_MIN
double(negative)           -2.22507e-308 to -1.79769e+308    
  

Example :

  
float val1 = 21.25;
double val2 = 1531.24595;
double val3 = 21.34E14    // 45E12 is equal to 21.34 * 10^14
double val4 = 1.23E-12    // 45E12 is equal to 1.23 * 10^-12    
  

Char

  • Its size is 1 byte.
  • Characters in C++ are enclosed inside single quotes ' '.

  
char c = 'a';
  

char val = ‘5’; // 5 stored as ascii character rather than int

Bool

  • Has a size of 1 byte
  • Used to store true/false values
  • Also used in conditional operations

  
bool val = false;
  

wchar_t

  • Wide character wchar_t is similar to the char data type
  • However, its size is 2 bytes instead of 1
  • These are used to represent characters that need more memory than 1 byte due to a large number of unique characters

  
wchar_t test = L'×?‎' // storing Hebrew character;
  

Void

  • Void is used with functions and pointers
  • They mean that they do not have any data type of value
  • We will learn more about it later

We cannot declare variables of the void type.