The compiler gives an error on this statement.
i>=3?b=10:b=5;
error: lvalue required as left operand of assignment
Not able to figure out why. Compiler being used is GCC.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The problem is explained by operator precedence. In accordance with C grammar your
is interpreted by C compiler as
Firstly, this is probably not what you intended. Secondly, in C language the result of
?:operator is not an lvalue. You cannot assign anything to it. Hence the error message.In order to correct the problem (assuming I understand your intent correctly), you either have to use braces
or simply rewrite it in a more conventional way
As a side note, this happens to be one of the differences between C and C++ grammar. In C++ your original statement would be interpreted as
i >= 3 ? (b = 10) : (b = 5)even without explicit braces. On top of that, in C++ the result of?:can be an lvalue.In other words, in C++ your original statement would compile and work “as intended”, but not in C.