Itachi Uchiwa
Itachi Uchiwa

Reputation: 3164

Is the result of the conditional operator an lvalue or rvalue?

We know that the conditional operator returns an lvalue if its Expr2 and Expr3 are lvalues of the same type otherwise it returns an rvalue:

int main(){
   int a = 0, b = 0;
   (a == 1 ? a : b) = 1; // C++ OK. C: error. lvalue needed

   return 0;
}

Here is the output on C:

main.c: In function ‘main’:
main.c:8:20: error: lvalue required as left operand of assignment
(a == 1 ? a : b) = 1;
                ^

Upvotes: 2

Views: 294

Answers (4)

Eric Postpischil
Eric Postpischil

Reputation: 224546

In C, the result1 of the conditional operator is an rvalue (a “value” in the terminology of the C standard) because C 2018 6.3.2.1 2 says:

Except when it is the operand of the sizeof operator, the unary & operator, the ++ operator, the -- operator, or the left operand of the . operator or an assignment operator, an lvalue that does not have array type is converted to the value stored in the designated object (and is no longer an lvalue);…

Thus, while the result of the conditional operator is its second or third operand, that operand has been converted to a value and is no longer an lvalue.

As M.M notes, you can get the same effect as selecting an lvalue by selecting addresses of operands instead and then applying *:

*(a == 1 ? &a : &b) = 1;

Footnote

1 The result of the conditional operator is not a return value, as there is no function call involved, so there is no call-and-return, just an expression evaluation.

Upvotes: 4

Генс
Генс

Reputation: 93

expression1? expression2 : expression3 If expression 1 has a true (non-zero) value, then the entire conditional expression takes the same value as expression 2 . If expression 1 has a false (null) value, then the entire conditional expression gets the same value as expression 3.

P r i m e r s
(5 > 3) ? 1 : 2 gets the value 1.
(3 > 5) ? 1 ; 2 gets the value 2.
(a > b) ? a : b gets a larger value among a or b. 
   The conditional operation itself is not a function that 
  returns a value.

Upvotes: -2

Adrian Mole
Adrian Mole

Reputation: 51905

In this C11 Draft Standard, there is a footnote that makes it clear that the result of a conditional operator is not an lvalue:

6.5.15 Conditional operator


Semantics

4    The first operand is evaluated; there is a sequence point between its evaluation and the evaluation of the second or third operand (whichever is evaluated). The second operand is evaluated only if the first compares unequal to 0; the third operand is evaluated only if the first compares equal to 0; the result is the value of the second or third operand (whichever is evaluated), converted to the type described below.110)


110) A conditional expression does not yield an lvalue.

Furthermore, in this later draft Standard (dated March, 2019), the wording hasn't changed significantly (except that the footnote is therein numbered 116, rather than 110).

Upvotes: 3

dbush
dbush

Reputation: 225767

This is one of those cases where C++ and C differ. In C the conditional operator results in an rvalue and therefore it cannot be the left side of an assignment.

Upvotes: 1

Related Questions