Conditional Operator:  ? :

conditional expression :

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

The conditional operator is a ternary operator (it takes three operands).

The logical-OR-expression must have integral, floating, or pointer type. It is evaluated in terms of its equivalence to 0. A sequence point follows logical-OR-expression.

Note that either expression or conditional-expression is evaluated, but not both.

In the type comparison for pointers, any type qualifiers (const or volatile) in the type to which the pointer points are insignificant, but the result type inherits the qualifiers from both components of the conditional.

Example

In the following example, nAcctBal is checked to see if it is below zero. If so, nTotalDue is set to zero. If not, nTotalDue is set to the value in nAcctBal.

// Example of the conditional operator
int nTotalDue, nAcctBal=200;

nTotalDue = (nAcctBal < 0) ? 0 : nAcctBal;   // nTotalDue now is 200