Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why would you use the ternary operator without assigning a value for the "true" condition (x = x ?: 1)

In the Android open-source qemu code I ran across this line of code:

machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */ 

Is this just a confusing way of saying:

if (machine->max_cpus) {    ; //do nothing } else {  machine->max_cpus = 1; } 

If so, wouldn't it be clearer as:

if (machine->max_cpus == 0) machine->max_cpus = 1; 

Interestingly, this compiles and works fine with gcc, but doesn't compile on http://www.comeaucomputing.com/tryitout/ .

like image 955
RickNotFred Avatar asked May 10 '10 20:05

RickNotFred


People also ask

Why would you use the ternary operator?

The ternary operator is an operator that exists in some programming languages, which takes three operands rather than the typical one or two that most operators use. It provides a way to shorten a simple if else block.

Why use ternary operator instead of if else?

The conditional operator – also known as the ternary operator – is an alternative form of the if/else statement that helps you to write conditional code blocks in a more concise way. First, you need to write a conditional expression that evaluates into either true or false .

Can we assign values in ternary operator?

The conditional ternary operator in JavaScript assigns a value to a variable based on some condition and is the only JavaScript operator that takes three operands. result = 'somethingelse'; The ternary operator shortens this if/else statement into a single statement: result = (condition) ?

Can you have ternary operator without else?

A ternary operation is called ternary because it takes 3 arguments, if it takes 2 it is a binary operation. It's an expression returning a value. If you omit the else you would have an undefined situation where the expression would not return a value. You can use an if statement.


2 Answers

This is permitted in GNU as an obscure extension to C

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 

In this simple case, the ability to omit the middle operand is not especially 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.

As you can probably guess, avoiding this is recommended for readability and portability reasons. I'm honestly surprised to see such a grammar-incompatible extension to C.

like image 100
Uri Avatar answered Sep 22 '22 00:09

Uri


This is a GCC extension that means "if the condition is true, use it, else use this other value", so

machine->max_cpus = machine->max_cpus ?: 1; 

is shorthand for

machine->max_cpus = machine->max_cpus ? machine->max_cpus : 1; 

although if the conditional has side-effects, it will only be run once

like image 29
Michael Mrozek Avatar answered Sep 22 '22 00:09

Michael Mrozek