in Programming edited by
285 views
2 votes
2 votes
#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 ??

in Programming edited by
285 views

Subscribe to GO Classes for GATE CSE 2022

2 Answers

5 votes
5 votes
 
Best answer

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));
selected by
0 votes
0 votes
  • 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

Related questions