7

I would like to know if in the ternary operator in language C all parameters are mandatory? e.g.:

(exp1 ? : value2);

or you need to write:

(expr1 ? value1: value2);

I asked that because if you write: (exp1 ? : value2); What return if the expr1 is TRUE?

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
anna coobs
  • 111
  • 5
  • 2
    Possible duplicate of [C conditional operator ('?') with empty second parameter](http://stackoverflow.com/questions/10143125/c-conditional-operator-with-empty-second-parameter) – msc Aug 31 '16 at 04:20

3 Answers3

5

It not a standard, but GCC extension (may be some other compilers do the same):

5.7 Conditionals with Omitted Operands

The middle operand in a conditional expression may be omitted. Then if the first operand is nonzero, its value is the value of the conditional expression.

Therefore, the expression

 x ? : y 

has the value of x if that is nonzero; otherwise, the value of y.

This example is perfectly equivalent to

 x ? x : y

Edit:

As @MadPhysicist pointed, that shortened form would evaluate the x once, while the traditional form would re-evaluate the x second time when x is non-zero

Serge
  • 6,088
  • 17
  • 27
4

The C standard (referring to C11) does not mention anything about omitting the second operand. It specifies about all the three operands for the form

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

You are talking about a compiler extension. The form

 (exp1 ? : value2);

actually returns exp1, if exp1 evaluates to TRUE.

Quoting the online documentation for gcc,

The middle operand in a conditional expression may be omitted. [...]

Therefore, the expression

    x ? : y

has the value of x if that is nonzero; otherwise, the value of y.

Just to add a bit of context in why or when this can be useful,

[...] When it becomes useful is when the first operand does, or may (if it is a macro argument), contain a side effect. Then repeating the operand in the middle would perform the side effect twice. Omitting the middle operand uses the value already computed without the undesirable effects of recomputing it.

Community
  • 1
  • 1
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • Can we skip the if-false part too? – Eugene Sh. Aug 30 '16 at 16:22
  • NVM, the citation in Serge answer is clarifying it. I asked if the `value2` can be omitted. – Eugene Sh. Aug 30 '16 at 16:24
  • @EugeneSh. Fine. Just added a bit more context. Hope that's helpful. :) – Sourav Ghosh Aug 30 '16 at 16:28
  • In this case (the last paragraph) it would be incorrect to say that it is equivalent to `x ? x : y` @Serge – Eugene Sh. Aug 30 '16 at 16:33
  • @EugeneSh. That example is in context of the actual example in the online docs. it also mentions _"In this simple case, the ability to omit the middle operand is not especially useful....."_ :) – Sourav Ghosh Aug 30 '16 at 16:38
  • @SouravGhwhosh One question more, you say "has the value of x if that is nonzero" What happen if you write X?:y and x is zero? – anna coobs Aug 30 '16 at 17:08
  • @annacoobs then, as per the property of ternary operator, the third operand `y` is evaluated and the value of that becomes the result. :) – Sourav Ghosh Aug 30 '16 at 17:11
  • 1
    Even if compilers add extensions, it is highly recommended not to use them as they break portability and compatibility of your code across the various compilers. Most of GCC's extensions have been created for the linux operating system (while this one doesn't seem to fix a technical issue the other extensions had). So use your keyboard and don't be afraid of 1~20 more keystrokes. – abidon Aug 30 '16 at 17:11
4

The statement

(exp1 ? : value2);   

is equivalent to

(exp1 ? exp1 : value2);

It is a GCC extension. The only difference in both of them is that exp1 will be evaluated only once in the former statement.

haccks
  • 104,019
  • 25
  • 176
  • 264