类型限定符Type Qualifiers

类型限定符为标识符提供两个属性之一。Type qualifiers give one of two properties to an identifier. const 类型限定符将对象声明为不可修改。The const type qualifier declares an object to be nonmodifiable. volatile 类型限定符声明一个项,该项的值可由超出该项所在的程序控制范围的某个项(如并发执行的线程)合理更改。The volatile type qualifier declares an item whose value can legitimately be changed by something beyond the control of the program in which it appears, such as a concurrently executing thread.

const 和 volatile 这两个类型限定符在声明中只能出现一次。The two type qualifiers, const and volatile, can appear only once in a declaration. 类型限定符可与任何类型说明符一起出现;但是,它们不能在多项声明中的第一个逗号的后面出现。Type qualifiers can appear with any type specifier; however, they cannot appear after the first comma in a multiple item declaration. 例如,以下声明是合法的:For example, the following declarations are legal:

typedef volatile int VI;  
const int ci;  

以下声明是非法的:These declarations are not legal:

typedef int *i, volatile *vi;  
float f, const cf;     

仅当访问作为表达式的左值的标识符时,类型限定符才会相关。Type qualifiers are relevant only when accessing identifiers as l-values in expressions. 有关左值和表达式的信息,请参阅左值表达式和右值表达式See L-Value and R-Value Expressions for information about l-values and expressions.

语法Syntax

type-qualifier:type-qualifier:
constvolatileconstvolatile

以下是合法的 const 和 volatile 声明:The following are legal const and volatile declarations:

int const *p_ci;       /* Pointer to constant int */  
int const (*p_ci);     /* Pointer to constant int */  
int *const cp_i;       /* Constant pointer to int */  
int (*const cp_i);     /* Constant pointer to int */  
int volatile vint;     /* Volatile integer        */  

如果数组类型的规范包括类型限定符,则将限定元素而不是数组类型。If the specification of an array type includes type qualifiers, the element is qualified, not the array type. 如果函数类型的规范包括限定符,则行为是不确定的。If the specification of the function type includes qualifiers, the behavior is undefined. volatile 和 const 都不会影响值的范围或对象的算术属性。Neither volatile nor const affects the range of values or arithmetic properties of the object.

此列表描述如何使用 const 和 volatileThis list describes how to use const and volatile.

  • const 关键字可用于修改任何基本或聚合类型、指向任何类型的对象的指针或 typedefThe const keyword can be used to modify any fundamental or aggregate type, or a pointer to an object of any type, or a typedef. 如果仅使用 const 类型限定符声明项,则项的类型将被当成 const int。可以初始化 const 变量,也可以将该变量置于存储的只读区域中。If an item is declared with only the const type qualifier, its type is taken to be const int. A const variable can be initialized or can be placed in a read-only region of storage. const 关键字对于声明指向 const 的指针很有用,因为这需要函数不以任何方式更改指针。The const keyword is useful for declaring pointers to const since this requires the function not to change the pointer in any way.

  • 编译器假定可通过使用或修改 volatile 变量的值未知过程来访问该变量(在程序中的任意未知)。The compiler assumes that, at any point in the program, a volatile variable can be accessed by an unknown process that uses or modifies its value. 因此,无论是否在命令行上指定优化,都必须为对 volatile 变量的每个分配或引用生成代码,即使它似乎不起作用。Therefore, regardless of the optimizations specified on the command line, the code for each assignment to or reference of a volatile variable must be generated even if it appears to have no effect.

    如果单独使用 volatile,则假定 intIf volatile is used alone, int is assumed. volatile 类型说明符可用于提供对特定内存位置的可靠访问。The volatile type specifier can be used to provide reliable access to special memory locations. volatile 用于数据对象,这些对象可通过信号处理程序、并行执行程序或特定硬件(如内存映射的 I/O 控制寄存器)访问或更改。Use volatile with data objects that may be accessed or altered by signal handlers, by concurrently executing programs, or by special hardware such as memory-mapped I/O control registers. 可以为变量的生存期将变量声明为 volatile,也可以将单个引用强制转换为 volatileYou can declare a variable as volatile for its lifetime, or you can cast a single reference to be volatile.

  • 项可以同时为 const 和 volatile,在这种情况下,项无法通过其自己的程序进行合理修改,但可以通过某些异步进程进行修改。An item can be both const and volatile, in which case the item could not be legitimately modified by its own program, but could be modified by some asynchronous process.

请参阅See Also

声明和类型Declarations and Types