C++ modifier types


C++ allows modifiers to be placed before the char, int, and double data types. Modifiers are used to change the meaning of a basic type so it better meets the needs of various situations.

The data type modifiers are listed below:

  • signed

  • unsigned

  • long

  • short

Modifierssigned, unsigned, long and short can be applied to integers, signed and unsigned can be applied to character types, and long can be applied to double types. The

modifiers signed and unsigned can also be used as prefixes for the long or short modifiers. For example: unsigned long int.

C++ allows the use of shorthand notation to declare unsigned short or unsigned long. Instead of int, you can write the words unsigned, short or unsigned, long, int is implicit. For example, both statements below declare unsigned integer variables.

unsigned x;
unsigned int y;

In order to understand the difference between C++ interpretation of signed integer and unsigned integer modifiers, let’s run the following short program:

#include <iostream>
using namespace std;
 
/* 
 * 这个程序演示了有符号整数和无符号整数之间的差别
*/
int main()
{
   short int i;           // 有符号短整数
   short unsigned int j;  // 无符号短整数

   j = 50000;

   i = j;
   cout << i << " " << j;

   return 0;
}

When the above program is run, it will be output The following results:

-15536 50000

In the above results, the bit pattern of the unsigned short integer 50,000 is interpreted as the signed short integer -15,536.

Type qualifiers in C++

Type qualifiers provide additional information about variables.

QualifierMeaning
constconst Objects of type cannot be modified during program execution.
volatileModifier volatile Tells the compiler that the value of a variable may be changed in ways not explicitly specified by the program.
restrictA pointer decorated with restrict is the only way to access the object it points to. Only C99 added the new type qualifier restrict.