The ternary (conditional) operator in C

Bongali Babu picture Bongali Babu · Apr 17, 2009 · Viewed 103.7k times · Source

What is the need for the conditional operator? Functionally it is redundant, since it implements an if-else construct. If the conditional operator is more efficient than the equivalent if-else assignment, why can't if-else be interpreted more efficiently by the compiler?

Answer

Charlie Martin picture Charlie Martin · Apr 17, 2009

In C, the real utility of it is that it's an expression instead of a statement; that is, you can have it on the right-hand side (RHS) of a statement. So you can write certain things more concisely.