The ternary (conditional) operator in C

105,678

Solution 1

The ternary operator is a syntactic and readability convenience, not a performance shortcut. People are split on the merits of it for conditionals of varying complexity, but for short conditions, it can be useful to have a one-line expression.

Moreover, since it's an expression, as Charlie Martin wrote, that means it can appear on the right-hand side of a statement in C. This is valuable for being concise.

Solution 2

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.

Solution 3

Some of the other answers given are great. But I am surprised that no one mentioned that it can be used to help enforce const correctness in a compact way.

Something like this:

const int n = (x != 0) ? 10 : 20;

so basically n is a const whose initial value is dependent on a condition statement. The easiest alternative is to make n not a const, this would allow an ordinary if to initialize it. But if you want it to be const, it cannot be done with an ordinary if. The best substitute you could make would be to use a helper function like this:

int f(int x) {
    if(x != 0) { return 10; } else { return 20; }
}

const int n = f(x);

but the ternary if version is far more compact and arguably more readable.

Solution 4

It's crucial for code obfuscation, like this:

Look->       See?!

No
:(
Oh, well
);

Solution 5

Compactness and the ability to inline an if-then-else construct into an expression.

Share:
105,678
Bongali Babu
Author by

Bongali Babu

Updated on July 08, 2022

Comments

  • Bongali Babu
    Bongali Babu almost 2 years

    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?

  • Darren Clark
    Darren Clark about 15 years
    I agree about the flogging, but I found that oddly readable. :) Surely on in test example with alphabetically aligned variables.
  • Darren Clark
    Darren Clark about 15 years
    This is THE point. It converts an if/else into an expression, NOT a statement. Somehow I suspect quite a few people here don't understand the difference(please refrain from commenting that YOU do, I'm not talking to you ;) ).
  • John Dibling
    John Dibling about 15 years
    Yeah, it gets real nasty when you start putting things in parenthesis.
  • John Feminella
    John Feminella about 15 years
    @Charlie: +1. I mentioned this in mine, but it's good to make this an explicit point.
  • Charlie Martin
    Charlie Martin about 15 years
    Well, const did come along about, oh, 25 years after the conditional operator. That is a cute trick though.
  • palantus
    palantus about 15 years
    Even one use can lead to bugs. Case in point: your release version will have the title "DEBUG App 1.0".
  • Artelius
    Artelius almost 15 years
    Note: to make the above code compile, just add struct{int See;}*Look;int No,Oh,well;int main(){ /* above code goes in here*/ }
  • Charles Bretana
    Charles Bretana almost 15 years
    And, because of this feature, it is a great tool to make code more "functional" and less "procedural".
  • Ether
    Ether over 14 years
    To continue the argument, we don't really need C at all because we can do everything necessary with assembler.
  • visual_learner
    visual_learner over 14 years
    "Portability is for people who cannot write new programs." - Linus Torvalds
  • Man Vs Code
    Man Vs Code over 12 years
    The inlining aspect is one distinct difference the others I think have overlooked.
  • dwn
    dwn almost 9 years
    Performance was one of its benefits during the rise of complex processors. You didn't have to dump the entire processor pipe to take a branch and then possibly perform an extra copy, instead it could often just push a single ready value into the pipe. Also, it is often more human-readable for multiline expressions than something like 'if (A) return ret1; else if (B) return ret2; ...'. There's nothing difficult to read in... return A? ret0 : B? ret1 : C? ret2 : D? ret3;
  • AlphaGoku
    AlphaGoku about 7 years
    The ternary operator also reduces Cyclomatic Complexity of the code.
  • John Feminella
    John Feminella about 7 years
    @AkshayImmanuelD ⇒ Ternary operators don't reduce cyclomatic complexity. The number of paths through the code is the same whether you use a ternary operator or an if statement.
  • Razzle
    Razzle about 3 years
    If you want to demonstrate use of the result as an l-value, shouldn't the ternary be on the left hand side of an assignment, for example?