C Programming - Control Instructions - Discussion

Discussion Forum : Control Instructions - Point Out Errors (Q.No. 9)
9.
Point out the error, if any in the program.
#include<stdio.h> 
int main()
{
    int a = 10, b;
    a >=5 ? b=100: b=200;
    printf("%d\n", b);
    return 0;
}
100
200
Error: L value required for b
Garbage value
Answer: Option
Explanation:

Variable b is not assigned.

It should be like:

b = a >= 5 ? 100 : 200;

Discussion:
34 comments Page 1 of 4.

Sandy said:   1 decade ago
It gives output as 100 when executed on DEV C++.

Prafull said:   1 decade ago
How the err "Error: L value required for b" comes? Can anyone explain for me?

Thanx in advance.

Vishal yadav said:   1 decade ago
It comes because on the left side of the assignment operator there are more than two variables having ? between the if you use (b=100) instead of it, there will be no error.

Ankit said:   1 decade ago
Still I don't understood.

Vadya said:   1 decade ago
int a = 10, b;
a >=5 ? b=100: 200;
printf("%d\n", b);

This code works.! Why.???
How is it equivalent to b=(a>=5?100:200) ?

Vinoth said:   1 decade ago
L value means left side of the expression is equal to b.so
b=(a>=5)?b=100:b=200;

Durga said:   1 decade ago
Hi this program explanation is not clear. Please send brief expl.

Mani said:   1 decade ago
Hi, You have to assign the value to any variable, but the exp a >=5 ? b=100: 200; is not assigned to any variable. So it gives the error msg of lvalue required.

Rajat Rahul said:   1 decade ago
Mani is correct.

Abhishek rai said:   1 decade ago
#include<stdio.h>
int main()
{
int a = 10, b;
a >=5 ? (b=100): (b=200);
printf("%d\n", b);
return 0;
}


Post your comments here:

Your comments will be displayed after verification.