error: lvalue required as left operand of assignment

I don't understand the error which the following program gives on compilation.

1
2
3
int a = 10,b;
    a >= 5 ? b = 100 : b= 200;
    printf("%d\n",b);


The error is "lvalue required as left operand of assignment"
Last edited on
Try using brackets:

int a = 10,b;
(a >= 5) ? b = 100 : b= 200;
printf("%d\n",b);
I understand that, but i want to know the reason why this error occurs, or what it means.
Last edited on
I think it may be because your compiler is misunderstanding the order of operations, so gets confused
The left side of an assignment operator must be an addressable expression.
Addressable expressions include the following:
numeric or pointer variables
structure field references or indirection through a pointer
a subscripted array element

http://docwiki.embarcadero.com/RADStudio/XE6/en/E2277_Lvalue_required_%28C%2B%2B%29
In C, the ternary operator has a higher priority than = and your code is parsed thusly:

(a >= 5 ? b = 100 : b) = 200 ;

In C++ it will work as expected since both operators have the same priority.
Topic archived. No new replies allowed.