6.6 Constant expressions

Previous Table of Contents "New C Standard" commentary

1322

constant-expression:
                conditional-expression

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

1324 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)

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

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

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

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

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

1330 More latitude is permitted for constant expressions in initializers.

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

1332 —  an arithmetic constant expression,

1333 —  a null pointer constant,

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

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

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

1337 —  an address constant, or

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

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

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

1341 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;

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

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

1344 An implementation may accept other forms of constant expressions.

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

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

1347 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: 2008-01-30 02:39:43 The text from WG14/N1256 is copyright © ISO