If the following code works
i=1;
i<10 ? printf("Hello") : printf("Bye");
then the assignment should also work.What is the reason which makes it to produce error?
i<10 ? foo=10 : foo=12;
If the following code works
i=1;
i<10 ? printf("Hello") : printf("Bye");
then the assignment should also work.What is the reason which makes it to produce error?
i<10 ? foo=10 : foo=12;
What is the reason which makes it to produce error?
Operator precedence.
i<10 ? foo=10 : foo=12;
is equivalent to (i<10 ? foo=10 : foo) = 12;
Use parentheses to fix your issue:
i<10 ? (foo=10) : (foo=12);
The reason is operator precedence. The following will work:
i<10 ? (foo=10) : (foo=12);
Your original expression gets parsed as
(i<10 ? foo=10 : foo)=12;
giving rise to the error (lvalue required as left operand of assignment
).