Home > C++ > Modifiers in C++

Modifiers are used to change meanings of base data types and expand them to a much larger set to use it according to the situations where it is used. C++ modifiers can be used with different data types available. It allows the base data types to have modifiers precede them.There are different types of modifiers in c++.

Modifiers in c++

  • Signed
  • Unsigned
  • Long
  • Short

All these can be used with integer data type.
Long and short are used to change/modify maximum and minimum values that a variable can hold.
Their size hierarchy is as:

  • short int< int < long int

Signed types include both positive and negative numbers and are the default type.
Unsigned numbers are always without any sign, that’s why they are always positive.

we can use signed and unsigned with long and short modifiers as well. e.g. unsigned short int or signed long int

Below table shows their size in bytes and their range for int data type:

TypeApproximate Size (bytes)Range
short2-32768 to 32767
unsigned short20 to 65,535
signed short2same as short
int2-32768 to 32767
unsigned int20 to 65,535
signed int2same as int
long4-2,147,483,648 to 2,147,483,647
unsigned long40 to 4,294,967,295
signed long4same as long

Below table shows their size in bytes and their range for char data type:

TypeApproximate Size (bytes)Range
char1-128 to 127
unsigned char10 to 255
signed char1same as char

Note: – long and short cannot be used with character data type.

For floating point values data type their size hierarchy is:

  • float < double < long double

Below table shows their size in bytes and their range for float data type:

TypeApproximate Size (bytes)RangeDigits of Precision
float43.4 × 10-38 to 3.4 × 1038 – 17
double81.7 × 10-308 to 1.7 × 10308 – 115
long double103.4 × 10-4932 to 3.4 × 104932 – 119

Note: -long float is not a legal type as it is same as double and is removed by ANSI similarly there are no short floating point numbers.
E.g.

#inlcude "stdafx.h"
#include "iostream"
using namespace std;
void main()
{
     short int v = 32767;
     unsigned short int w = 65535;
     int x = 2147483647;
     long int y = 2147483647;
     unsigned long int z = 4294967295;

     cout << "Max Value of Short Int = " << v << endl;
     cout << "Max Value of Unsigned Short Int = " << w << endl;
     cout << "Max Value of Int = " << x << endl;
     cout << "Max Value of Long Int = " << y << endl;
     cout << "Max Value of unsigned Long Int = " << z << endl;
}

Output: –

modifiers in c++ example

Leave a Reply

Your email address will not be published. Required fields are marked *