4

I'm new to C and today I learnt "?" operator which is the short type of if-else statement. However, when I execute this code:

int b;
int x;
b=3<2?x=12:x=34;

I get an error "error: lvalue required as left operand of assignment". I don't understand why it happens. Process in my mind is that the program first assigns 34 to x, then it assigns value of x,which is 34, to b. On the other hand, I can use the statement as

int b;
int x;
b=3<2?x=12:(x=34);

without any errors. I looked to my book but nothing helped. Why can't I use the first statement? What is my computer trying to do? Thanks...

M.M
  • 138,810
  • 21
  • 208
  • 365
F. Eser
  • 125
  • 9
  • 2
    operator precedence perhaps? – Jean-François Fabre Nov 02 '16 at 20:01
  • That is a ternary operator, `?` (some_condition) `:` (other_condition). – t0mm13b Nov 02 '16 at 20:01
  • 1
    If you want a side-effect in `x`, with that value assigned to `b` you need parentheses around *both* the alternate expressions. `b = 3 < 2 ? (x=12) : (x=34);` Try adding `printf("%d %d\n", b, x);` after that ternary operator. – Weather Vane Nov 02 '16 at 20:03
  • 1
    "short type of if-else" - It can be **miss**used as that, yes. But don't even think about! And use spaces around such operators. What is your code supposed to do? Looks like you have not really understood what the conditional operator does. – too honest for this site Nov 02 '16 at 20:06
  • 3
    still, I answered but deleted since operator precendence is not the reason here: "The expression in the middle of the conditional operator (between ? and :) is parsed as if parenthesized: its precedence relative to ?: is ignored". So if it's parsed as if parenthesized why does it need parentheses (I agree that the construct is not very good, this is not an if else but ternary operator) – Jean-François Fabre Nov 02 '16 at 20:08
  • 1
    It would probably be clearer written as: `b = x = (3 < 2) ? 12 : 34;`. – Jonathan Leffler Nov 02 '16 at 20:14
  • Just do one thing at a time – Ed Heal Nov 02 '16 at 20:15
  • @Olaf I don't know what the code is for but it was my exam question. I tried it with gcc and saw the result. – F. Eser Nov 02 '16 at 20:15
  • You first should understand what your code is **supposed** to do. Without that it is pointless asking how to fix an error. As given, the code is nonsense and no sane programmer would write such code. – too honest for this site Nov 02 '16 at 20:17
  • 2
    Could the compiler be evaluating as `b = (3<2?x=12:x) = 34;` That would explain the "lvalue required" error. – John D Nov 02 '16 at 20:18
  • @JohnD uhh finally understand. Thank you... – F. Eser Nov 02 '16 at 20:21
  • That was a guess on my part - can anyone confirm it? – John D Nov 02 '16 at 20:23

1 Answers1

3

+1 for interesting question - it highlights two differences between C++ and C.

(1) The evaluation rules for ternary expressions are different in C and C++

C++ parses as follows

logical-OR-expression ? expression : assignment-expression

It is therefore parsing your statement by matching assignment-expression to x=34

b = 3<2 ? x = 12 : (x = 34);

But C parses like this

logical-OR-expression ? expression : conditional-expression 

x = 34 is not a conditional-expression so your statement gets parsed like

b = (3<2 ? x = 12 : x) = 34;

(2) The conditional operator in C++ can return an lvalue, whereas C cannot. Hence, the following is legal in C++ but not in C:

b = (3<2 ? x = 12 : x) = 34;

Verified on ideone.com for C and C++ compilers. See also these links Errors using ternary operator in c for diff between C and C++ ternary operator Conditional operator differences between C and C++ for diff in lvalue rules

Community
  • 1
  • 1
John D
  • 1,627
  • 1
  • 11
  • 10
  • ideone suppresses a lot of error message, so it's not a great tool for "verification". Suggest using gcc.godbolt.org – M.M Nov 02 '16 at 21:38