• retagged by
852 views
0 0 votes

P1:

int main()
{
int a,b,c;
a = 10;
a>=5? b=100 : b=200;
printf("%d %d",a,b);
    return 0;
}

P2:

int main()
{
int a,b,c;
a = 10;
a>=5? b=100 : (b=200);
printf("%d %d",a,b);
    return 0;
}

P1 gives error as

lvalue required as left operand of assignment
 a>=5? b=100 : b=200;
                ^

while P2 doesn't!

Please explain why?

1 Answer

Best answer
2 2 votes

precedence of RELATIONAL OPERATOR> TERNARY OPERATOR > ASSIGNMENT OPERATOR

therefor in our case evaluation order should be (>=) first the (?:) then (=)

according to this order of precedence ,  in P1 , expression a>=5? b=100 : b=200;  is evaluated as

((a>=5)?b=100:b)=200 which is invalid because lefthand side should be a variable to store 200 hence ur getting error of lvalue required. while this is not an issue in P2

• selected by
Position:
Show:

Related questions

1 1 vote
1 1 answer
177
177 views
khanshahima4 asked Sep 24
177 views
1. If precedence tells me which operator has priority, and associativity tells me how to handle operators at the same level, then why do I need a separate concept called ...
14 14 votes
1 1 answer
1.3k
1.3k views
GO Classes asked Jul 2
1,272 views
Assume:$\texttt{short = 2 bytes}$, $\texttt{int = 4 bytes}$, $\texttt{char = 1 byte}$$\texttt{int}$ needs $4$-byte alignment and the final structure size is rounded to a ...
17 17 votes
2 2 answers
686
686 views
GO Classes asked Jul 2
686 views
Assume:struct Student { double gpa; }; struct Student alice; struct Student *sptr = &alice;Which of the following correctly assigns $\texttt{4.0}$ to $\texttt{alice.gpa}$...
10 10 votes
1 1 answer
515
515 views
GO Classes asked Jul 2
515 views
Consider the following code idea from the source:typedef struct { double x, y; } Point; void reset(Point p) { p.x = p.y = 0; } int main() { Point a = {12.0, 42.0}; Point ...