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.

data

Information stored while writing a code 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-Defined
Data Types in C++ New
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
Primitive Data Types (Built-in)
  • Boolean
  • Character
  • Integer
  • Floating point
  • Double floating point
  • Void
  • Wide character

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

Data Types in C++Short NameStorage Type
BooleanboolBoolean or logical values like True/False
CharactercharCharacters values
IntegerintInteger values
Floating PointfloatSingle precision Decimal Values
Double Floating PointdoubleDouble precision Decimal Values
voidvoidValueless entity / Null type
Wide characterwchar_tCharacter data type with larger values

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

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
short int2 bytes-32,768 to 32,767
signed short int
2 bytes-32,768 to 32,767
unsigned short int2 bytes0 to 65,535
int2 or 4 bytes-32768 to +32768
or -2,147,483,648 to +2,147,483,648
signed int2 or 4 bytes-32768 to +32768
or -2,147,483,648 to +2,147,483,648
unsigned int2 or 4 bytes0 to +65,535
or 0 to +4,294,967,295
long int4 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 int4 or 8 bytes0 to 4,294,967,295
or 0 to 18,446,744,073,709,551,615
unsigned long int4 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
float4 bytes1.17549e-38 to 3.40282e+38
Double Floating Point8 bytes2.22507e-308 to 1.79769e+308
Long Double Floating Point16 bytes-3.3621e-4932 to +3.3621e-4932
No ValueNot validNot valid
Wide character2 or 41 wide character
C++ Code for Sizes
The following code will give you the output of the size of each data type –
Run
// 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

Understanding Each Datatype

We will look at all different data types in C++ :
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              -DBL_MIN

An example of initializing variables would be –

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 ' '.

For example –

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

Example –

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

Example –

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.

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription