Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

The type of modifier for C+


May 11, 2021 C++


Table of contents


The type of modifier for the C++ modifier

C++ allows modifiers to be placed before char, int, and double data types. Modifiers are used to change the meaning of the base type, so they are more responsive to the needs of a variety of situations.

The data type modifiers are listed below:

  • signed
  • unsigned
  • long
  • short

Modifiers signed, unsigned, long, and short can be applied to integers, signed and unsigned can be applied to character types, and long can be applied to doubles.

Modifiers signed and unsigned can also be prefixed as long or short modifiers. For example: unsigned long int.

Shorthand symbols are allowed to be used to declare unsigned short integers or unsigned long integers. Y ou can write no int, only the word un shortsigned or unsigned long, int is implied. For example, the following two statements declare unsigned integer variables.

unsigned x;
unsigned int y;

To understand the difference between a signed integer and an unsigned integer modifier, 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 program above runs, the following results are output:

-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.

The type qualifier in C+

Type qualifiers provide additional information about variables.

Default meaning
const const Type objects cannot be modified during program execution.
volatile Modifier volatile Tell the compiler, the value of the variable may be changed in a manner that the program is not explicitly specified.
restrict Depend on restrict The modified pointer is the only way to access the object it points to.Only C99 adds a new type of qualifier Restrict.