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:
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:
- signed, unsigned, long, and short can be applied to integer base types.
- signed and unsigned can be applied to char
- long can be applied to double.
- signed and unsigned can also be used as a prefix to long or short modifiers.
modifier base data type = value;
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.
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.
/* This program shows the difference between
- signed and unsigned integers.
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:
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.