C 常量表达式C Constant Expressions

常量表达式将在编译时而不是运行时计算,并且可在可使用常量的任何位置使用。A constant expression is evaluated at compile time, not run time, and can be used in any place that a constant can be used. 常量表达式的计算结果必须是位于该类型的可表示值范围内的常量。The constant expression must evaluate to a constant that is in the range of representable values for that type. 常量表达式的操作数可以是整数常量、字符常量、浮点常量、枚举常量、类型强制转换、sizeof 表达式和其他常量表达式。The operands of a constant expression can be integer constants, character constants, floating-point constants, enumeration constants, type casts, sizeof expressions, and other constant expressions.




logical-OR-expression ?logical-OR-expression ? expression : conditional-expressionexpression : conditional-expression


expression , assignment-expressionexpression , assignment-expression


unary-expression assignment-operator assignment-expressionunary-expression assignment-operator assignment-expression

assignment-operator: one ofassignment-operator: one of
= *= /= %= += -= <<= >>= &= ^= |== *= /= %= += -= <<= >>= &= ^= |=

结构声明符、枚举数、直接声明符、直接抽象声明符和标记语句的非终止符包含 constant-expression 非终止符。The nonterminals for struct declarator, enumerator, direct declarator, direct-abstract declarator, and labeled statement contain the constant-expression nonterminal.

整数常量表达式必须用于指定结构的位域成员的大小、枚举常量的值、数组的大小或 case 常量的值。An integral constant expression must be used to specify the size of a bit-field member of a structure, the value of an enumeration constant, the size of an array, or the value of a case constant.

预处理器指令中使用的常量表达式受其他限制的约束。Constant expressions used in preprocessor directives are subject to additional restrictions. 因此,它们被称为“受限制的常量表达式”。Consequently, they are known as "restricted constant expressions." 受限制的常量表达式不能包含 sizeof 表达式、枚举常量、到任何类型的类型强制转换或浮点类型常量。A restricted constant expression cannot contain sizeof expressions, enumeration constants, type casts to any type, or floating-type constants. 但它可包含特殊常量表达式 defined (identifier)It can, however, contain the special constant expression defined (identifier).

请参阅See Also

操作数和表达式Operands and Expressions