Go Up to Binary Operators Index
logical-OR-expression ? expression : conditional-expression
The conditional operator ?: is a ternary operator.
In the expression
E1 ? E2 : E3,
E1 evaluates first. If its value is true, then
E2 evaluates and
E3 is ignored. If
E1 evaluates to false, then
E3 evaluates and
E2 is ignored.
The result of
E1 ? E2 : E3 will be the value of either
E3 depending upon which
E1 must be a scalar expression.
E3 must obey one of the following rules:
- Both of arithmetic type.
E3are subject to the usual arithmetic conversions, which determines the resulting type.
- Both of compatible struct or union types. The resulting type is the structure or union type of
- Both of void type. The resulting type is void.
- Both of type pointer to qualified or unqualified versions of compatible types. The resulting type is a pointer to a type qualified with all the type qualifiers of the types pointed to by both operands.
- One operand is a pointer, and the other is a null pointer constant. The resulting type is a pointer to a type qualified with all the type qualifiers of the types pointed to by both operands.
- 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. The resulting type is that of the non-pointer-to-void operand.