Monday 10 October 2022

Data Types in C++

                     In this post, we will see Data Types in C++.


Data Type Definition:

                     Data type is an attribute of variables which defines what values variable can have and what operations can be performed on these variables.


int variable can have value 3, 7,13 etc. while float can have value 31.4, 5.2 etc. 

We can perform addition, subtraction on int, float variables while we can negate Boolean variable. 


    Classification of Data Types in C++ 

The primitive data types are of three types:

1. Integral type, which means there is no decimal point

2. Boolean means true or false

3. Floating-point with the decimal point 

C++ Type Modifiers:

                                 By using Type Modifiers, we can can modify the data type of variables.

                                 Following are the Type Modifiers in C++:

1. signed  (can be applied to int, char)

2. unsigned  (can be applied to int, char)

3. short (can be applied to int)

4. long (can be applied to int, double)

                             C++ allows a shorthand notation for declaring unsigned, short, or long integers. That means, we can use them without int. 


unsigned x;

short x;

long x; 

Size of Data Types (in bytes):

Size of int: 4

Size of signed int: 4

Size of unsigned int: 4

Size of short int: 2

Size of long int: 4

Size of long long int: 8

Size of char: 1

Size of signed char: 1

Size of unsigned char: 1

Size of float: 4

Size of double: 8

Size of long double: 12

Size of bool: 1

Range of possible values:


int Data Type

Size 4 bytes i.e. 32 bits


Total numbers possible

1 bit = 21 i.e. 2 numbers

2 bits = 22 i.e. 4 numbers

3 bits = 23 i.e. 8 numbers


32 bits = 232 numbers


Unsigned int 

Range 0 to 232 – 1


Signed int   

1 bit is reserved for sign

That’s why usable bits are 31 bits

Range -231 to 0 to 231-1 

Program Code: (datatypes.cpp)


using namespace std;

int main()


int a;

signed int b;

unsigned int c;

short int d;

long int e;

char f;

signed char g;

unsigned char h;

float i;

double j;

long double k;

bool l;

cout<<"Size of int: "<<sizeof(int)<<endl;

cout<<"Size of signed int: "<<sizeof(signed int)<<endl;

cout<<"Size of unsigned int: "<<sizeof(unsigned int)<<endl;

cout<<"Size of short int: "<<sizeof(short int)<<endl;

cout<<"Size of long int: "<<sizeof(long int)<<endl;

cout<<"Size of long long int: "<<sizeof(long long int)<<endl;

cout<<"Size of char: "<<sizeof(char)<<endl;

cout<<"Size of signed char: "<<sizeof(signed char)<<endl;

cout<<"Size of unsigned char: "<<sizeof(unsigned char)<<endl;

cout<<"Size of float: "<<sizeof(float)<<endl;

cout<<"Size of double: "<<sizeof(double)<<endl;

cout<<"Size of long double: "<<sizeof(long double)<<endl;

cout<<"Size of bool: "<<sizeof(bool)<<endl;

return 0;





No comments:

Post a Comment