edited by
421 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 ??

edited by

2 Answers

Best answer
5 votes
5 votes

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);

Related questions

3 votes
3 votes
5 answers
1
arpit.bagri asked Aug 6, 2023
1,042 views
Why is the output of the below program 36? int main(){ int a = 1; int b = ++a * ++a * ++a; printf("%d", b ); ​​​​ return 0; }
5 votes
5 votes
2 answers
2
saurabh0709 asked Aug 1, 2023
1,136 views
What will be the output of the following code? _______ #include <stdio.h int main(){ char val=250; int ans; ans= val+ !val + ~val + ++val; printf("%d", ans); return 0; }
1 votes
1 votes
5 answers
3
1 votes
1 votes
3 answers
4
Ram Swaroop asked Dec 18, 2018
393 views
Which operator has higher priority between *and / in any expression how to evaluate them