[Main]
Title=Conditional operator ('? :')
Index=?, :

[Top]
The conditional operator <CODE>'?:'</CODE> is, in fact, a ternary operator.
It uses the following syntax:
<PRE><I>expr1</I> ? <I>expr2</I> : <I>expr3</I>
</PRE>
In the expression <I>expr1</I>&nbsp;?&nbsp;<I>expr2</I>&nbsp;:&nbsp;<I>Expr3</I>, the operand <I>expr1</I> must be of scalar type. The
operands <I>expr2</I> and <I>Expr3</I> must obey one of the following sets of rules:
<OL>
<LI>
Both of arithmetic type. In this case, both <I>expr2</I> and <I>Expr3</I> are subject
to the usual arithmetic conversions, and the type of the result is the common type
resulting from these conversions.
</LI>
<LI>
Both of compatible structure or union types.
In this case, the type of the result is the structure or union type of <I>expr2</I> and <I>expr3</I>.
</LI>
<LI>
Both of void type. In this case, the result is of type void.
</LI>
<LI>
Both of type pointer to qualified or unqualified versions of compatible types.
In this case, the type of the result is pointer to a type qualified with all the
type qualifiers of the types pointed to by both operands.
</LI>
<LI>
One operand of pointer type, the other a null pointer constant
In this case, the type of the result is pointer to a type qualified with all the
type qualifiers of the types pointed to by both operands.
</LI>
<LI>
One operand of type pointer to an object, the other of type
pointer to a qualified or unqualified version of void.
In this case, the type of the result is that of the non-pointer-to-void operand.
</LI>
</OL>

In all cases, <I>expr1</I> is evaluated first. If its value is nonzero (true), then <I>expr2</I>
is evaluated and <I>expr3</I> is ignored (not evaluated at all). If <I>expr1</I> evaluates
to zero (false), then <I>expr3</I> is evaluated and <I>expr2</I> is ignored.
The result of <I>expr1</I>&nbsp;?&nbsp;<I>expr2</I>&nbsp;:&nbsp;<I>expr3</I> will be the value of whichever of <I>expr2</I> and
<I>expr3</I> is evaluated.
<BR><BR>
<B>Note:</B> GNU C extends the usage of the conditional operator to allow
<A HREF="$$INFOLINK(gnuexts/SEC71)">omitting the middle operand</A>, so it may be used as
a binary operator too.
