Modifier Types in C++

Written by

Vaaruni Agarwal

A modifier is used to alter the meaning of the base data type so that, the new data type thus generated can fit into the requirements in a better way.

C++ allows some of its basic data types, like the char, int, and double data types to have modifiers preceding them.

The data type modifiers allowed in C++ are listed here: 

  • signed 
  • unsigned 
  • long 
  • short 

When these modifiers are prefixed then, the data storage and the range of the given basic data type is altered. 

Following modifiers can be applied to the base data types:

  1. signed, unsigned, long, and short can be applied to integer base types. 
  2. signed and unsigned can be applied to char
  3. long can be applied to double
  4. signed and unsigned can also be used as a prefix to long or short modifiers.

Syntax:

modifier base data type = value;

For example:

unsigned long int = 556790;

 

C++ also allows a shorthand notation for declaring the unsigned, short, or long integers. 

One can simply use the word unsigned, short, or long, without writing the data type int. The compiler automatically implies int

For example:

unsigned x; 

unsigned int y;

Both of the statements above declare the unsigned int variables, x and y. These two statements are exactly the same, the first one is using the shorthand assignment while the second statement is complete in itself.

#include <iostream.h> 

/* This program shows the difference between 

  • signed and unsigned integers. 

*/ 

void main() 

short int i; // a signed short integer

short unsigned int j; // an unsigned short integer 

j = 50000; 

i = j; 

cout << i << "\n " << j; 

}

When this program is run, following is the output: 

-15536 

50000

 

The above result is because the bit pattern that represents 50,000 as a short unsigned integer is interpreted as -15,536 by a short. 

 

Modifier Types in C++