Latest web development tutorials

C ++ type modifiers

C ++ allows you to place a modifier in front ofchar, int and double data type.Modifiers for changing the meaning of the basic types, so it can better meet the needs of a variety of situations.

The following lists the data type modifier:

  • signed
  • unsigned
  • long
  • short

Modifierssigned, unsigned, long and short can be applied to integer,signed andunsignedmay be applied tochar,long can be applied to double.

Modifierssigned and unsignedmay be aslongorshortmodifier prefix. Forexample: unsigned long int.

C ++ allows the use of shorthand notation to declarean unsigned short or unsigned long integer.You can not write int, write only the wordunsigned, short, or unsigned, long, intis implied. For example, the following two statements both declare unsigned integer variables.

unsigned x;
unsigned int y;

To understand the difference between C ++ interpretation signed integer and unsigned integer qualifier between, 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 running, it will output the following results:

-15536 50000

These results, the unsigned short integer 50,000 bit patterns are interpreted as short signed integer -15,536.

In C ++ type qualifiers

Type Qualifiers provide additional information variables.

限定符含义
constconst类型的对象在程序执行期间不能被修改改变。
volatile修饰符volatile告诉编译器,变量的值可能以程序未明确指定的方式被改变。
restrictrestrict修饰的指针是唯一一种访问它所指向的对象的方式。只有 C99 增加了新的类型限定符 restrict。