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 4 of 4.

Maikal kumar said:   1 decade ago
Answer must be 100; because a=10 thats are >a. But please help me about lvalue value.

Sarang said:   1 decade ago
It gives output b = 200 on DOSBox c++ when the expression is
a >=5 ? b=100: b=200;
In above expression.if you change a>=11/12 or anything.
Ouptut is b = 200. any condition doesn't make any difference to this answer.

When expression is a >=5 ? (b=100): (b=200);
It gives output b = 100;

Nowhere error is coming, please somebody explain me

Umesh said:   1 decade ago
Actually when we use conditional operators we should store the condition result in any variable. see below example.
#include<stdio.h>
{
int a=10, b;
b= a>=5?100:200;
printf("%d", b);
}
RESULT:
100

The above given question should be like i have stated here.

#include<stdio.h>
{
int a=10, b;
a>=5?100:200;
printf("%d", b);
}
RESULT:
ERROR


According to the given question answer should be 100 not error because b value will be assigned if condition is true or false.

Subbu said:   1 decade ago
What is the meaning of ? in between the a>=5 and b=100.


Post your comments here:

Your comments will be displayed after verification.