6.5.15 Conditional operator

Previous Table of Contents "New C Standard" commentary

1264

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

1265 The first operand shall have scalar type.

1266 One of the following shall hold for the second and third operands:

1267 —  both operands have arithmetic type;

1268 —  both operands have the same structure or union type;

1269 —  both operands have void type;

1270 —  both operands are pointers to qualified or unqualified versions of compatible types;

1271 —  one operand is a pointer and the other is a null pointer constant; or

1272 —  one operand is a pointer to an object or incomplete type and the other is a pointer to a qualified or unqualified version of void.

1273 The first operand is evaluated;

1274 there is a sequence point after its evaluation.

1275 The second operand is evaluated only if the first compares unequal to 0;

1276 the third operand is evaluated only if the first compares equal to 0;

1277 the result is the value of the second or third operand (whichever is evaluated), converted to the type described below.93)

1278 If an attempt is made to modify the result of a conditional operator or to access it after the next sequence point, the behavior is undefined.

1279 If both the second and third operands have arithmetic type, the result type that would be determined by the usual arithmetic conversions, were they applied to those two operands, is the type of the result.

1280 If both the operands have structure or union type, the result has that type.

1281 If both operands have void type, the result has void type.

1282 If both the second and third operands are pointers or one is a null pointer constant and the other is a pointer, the result type is a pointer to a type qualified with all the type qualifiers of the types pointed-to by both operands.

1283 Furthermore, if both operands are pointers to compatible types or to differently qualified versions of compatible types, the result type is a pointer to an appropriately qualified version of the composite type;

1284 if one operand is a null pointer constant, the result has the type of the other operand;

1285 otherwise, one operand is a pointer to void or a qualified version of void, in which case the result type is a pointer to an appropriately qualified version of void.

1286 93) A conditional expression does not yield an lvalue.

1287 EXAMPLE The common type that results when the second and third operands are pointers is determined in two independent stages. The appropriate qualifiers, for example, do not depend on whether the two pointers have compatible types.

Given the declarations


        const void *c_vp;
        void *vp;
        const int *c_ip;
        volatile int *v_ip;
        int *ip;
        const char *c_cp;

the third column in the following table is the common type that is the result of a conditional expression in which the first two columns are the second and third operands (in either order):

c_vp      c_ip     const void *
v_ip      0        volatile int *
c_ip      v_ip     const volatile int *
vp        c_cp     const void *
ip        c_ip     const int *
vp        ip       void *

Next

Created at: 2008-01-30 02:39:43 The text from WG14/N1256 is copyright © ISO