Conditional Operators

From RAD Studio
Jump to: navigation, search

Go Up to Binary Operators Index

Syntax

logical-OR-expression ? expression : conditional-expression

Remarks

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 E2 or E3 depending upon which E1 evaluates.

E1 must be a scalar expression. E2 and E3 must obey one of the following rules:

  1. Both of arithmetic type. E2 and E3 are subject to the usual arithmetic conversions, which determines the resulting type.
  2. Both of compatible struct or union types. The resulting type is the structure or union type of E2 and E3.
  3. Both of void type. The resulting type is void.
  4. 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.
  5. 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.
  6. 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.