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

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

Output is 1 why? It must be 200 right.
(1)

Abcd said:   1 decade ago
Ternary operator ?: evaluates the condition (a>=5) if its true the it assigns (b=100) else it assigns (b=200).

We do not necessarily need a variable to store the output to store the expressions (a>=5? b=100:b=200) result.

So this cannot be the reason for LValue error. So whats the actual reason?

Nitesh said:   1 decade ago
It can also be like this.

#include<stdio.h>
int main()

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

Produces same o/p : 100

Prajyot said:   1 decade ago
Above code must be equivalent to following code as follows :

#include<stdio.h>
int main()
{
int a = 10, b;
if(a >=5)b=100;
else b=200;
printf("%d\n", b);
return 0;
}

b must have 100 value as if condition is satisfying.

Parveen Soni said:   1 decade ago
L value error is found because if you are writing a>=5?b=100:b=200;

Then in last assignment b=200,the value 200 is assigned to left side,which is not correct as per C-language Rule.

Hence,the above problem can be removed by using parenthesis on b=100 and b=200 [like (b=100) and (b=200)].

On other side,if you use expression as:

b=a>=5 ? b=100: b=200;

Then again it will show the same error because of same reason.

But expression is correct if you use:
b=a>=5 ? (b=100): (b=200);

Output is 100 when you use parenthesis.
Compiler used : Dev C++ version 5.4.1 Portable.

Priyanka K. said:   1 decade ago
Condition is satisfying, hence b=100 will be assigned to b and it becomes print 100?

Ajeet said:   1 decade ago
The conditional operator (?:) always return either true part or false part. Lvalue means legal left value. There no need of Lvalue in conditional operator so the program executes successfully and gives output 100;.

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

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.

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


Post your comments here:

Your comments will be displayed after verification.