6.6 Constant expressions

Previous Table of Contents

1312

constant-expression:
                conditional-expression

1313 A constant expression can be evaluated during translation rather than runtime, and accordingly may be used in any place that a constant may be.

1314 Constant expressions shall not contain assignment, increment, decrement, function-call, or comma operators, except when they are contained within a subexpression that is not evaluated.96)

1315 Each constant expression shall evaluate to a constant that is in the range of representable values for its type.

1316 An expression that evaluates to a constant is required in several contexts.

1317 If a floating expression is evaluated in the translation environment, the arithmetic precision and range shall be at least as great as if the expression were being evaluated in the execution environment.

1318 An integer constant expression97) shall have integer type and shall only have operands that are integer constants, enumeration constants, character constants, sizeof expressions whose results are integer constants, and floating constants that are the immediate operands of casts.

1319 Cast operators in an integer constant expression shall only convert arithmetic types to integer types, except as part of an operand to the sizeof operator.

1320 More latitude is permitted for constant expressions in initializers.

1321 Such a constant expression shall be, or evaluate to, one of the following:

1322 —  an arithmetic constant expression,

1323 —  a null pointer constant,

1324 96) The operand of a sizeof operator is usually not evaluated (6.5.3.4).

1325 97) An integer constant expression is 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.

1326 Further constraints that apply to the integer constant expressions used in conditional-inclusion preprocessing directives are discussed in 6.10.1.

1327 —  an address constant, or

1328 —  an address constant for an object type plus or minus an integer constant expression.

1329 An arithmetic constant expression shall have arithmetic type and shall only have operands that are integer constants, floating constants, enumeration constants, character constants, and sizeof expressions.

1330 Cast operators in an arithmetic constant expression shall only convert arithmetic types to arithmetic types, except as part of an operand to a sizeof operator whose result is an integer constant.

1331 An address constant is a null pointer, a pointer to an lvalue designating an object of static storage duration, or a pointer to a function designator;

1332 it shall be created explicitly using the unary & operator or an integer constant cast to pointer type, or implicitly by the use of an expression of array or function type.

1333 The array-subscript [] and member-access . and -> operators, the address & and indirection * unary operators, and pointer casts may be used in the creation of an address constant, but the value of an object shall not be accessed by use of these operators.

1334 An implementation may accept other forms of constant expressions.

1335 The semantic rules for the evaluation of a constant expression are the same as for nonconstant expressions.98)

1336 Forward references: array declarators (6.7.5.2), initialization (6.7.8).

1337 98) Thus, in the following initialization,


        static int i = 2  || 1 / 0;

the expression is a valid integer constant expression with value one.

Next

Created at: 2005-06-29 02:19:00 The text from WG14/N1124 is copyright © ISO