285 views
#include<stdio.h>

int main(void) {
int p=3,q=4,y,z;
(p>q?y=5:z=10);
printf(" %d %d ",y,z);
return 0;
}


Compile Info

prog.c: In function 'main':
prog.c:5:15: error: lvalue required as left operand of assignment
(p>q?y=5:z=10);
^

z is already a variable ,then why it is saying lvalue required ??

### Subscribe to GO Classes for GATE CSE 2022

Assignment has a lower precedence than the ternary operator so the line evaluates like:

((p>q?y=5:z)=10);

See how this statement is parsed by compiler :Here

lvalue means an assignable value (variable), and in assignment the left value to the = has to be lvalue.

So to get rid of use.

((p>q)?(y=5):(z=10));
by
• Assignment has a lower precedence than the ternary operator.
• so the line evaluates like:( (p>q)?y=5:z))=10;
• No error will come if you write the line p>q?(y=5):(z=10);
by

1 vote