Previous: , Up: Conditional Expression   [Contents][Index]

8.4.2 Conditional Operator Branches

We call iftrue and iffalse the branches of the conditional.

The two branches should normally have the same type, but a few exceptions are allowed. If they are both numeric types, the conditional converts both to their common type (see Common Type).

With pointers (see Pointers), the two values can be pointers to nearly compatible types (see Compatible Types). In this case, the result type is a similar pointer whose target type combines all the type qualifiers (see Type Qualifiers) of both branches.

If one branch has type void * and the other is a pointer to an object (not to a function), the conditional converts the void * branch to the type of the other.

If one branch is an integer constant with value zero and the other is a pointer, the conditional converts zero to the pointer’s type.

In GNU C, you can omit iftrue in a conditional expression. In that case, if condition is nonzero, its value becomes the value of the conditional expression, after conversion to the common type. Thus,

x ? : y

has the value of x if that is nonzero; otherwise, the value of y.

Omitting iftrue is useful when condition has side effects. In that case, writing that expression twice would carry out the side effects twice, but writing it once does them just once. For example, if we suppose that the function next_element advances a pointer variable to point to the next element in a list and returns the new pointer,

next_element () ? : default_pointer

is a way to advance the pointer and use its new value if it isn’t null, but use default_pointer if that is null. We cannot do it this way,

next_element () ? next_element () : default_pointer

because that would advance the pointer a second time.

Previous: , Up: Conditional Expression   [Contents][Index]