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.

Pranali said:   8 years ago
@Gourav.

Because you are printing the value of b, so it will print 1.

Rishikesh Sonawane said:   4 years ago
1) Ternary operator has high precedence than "=".
2) So the compiler is , taking it as , ((a >=5) ? b=100: b)=200;
Hence the error Lvalue required.

Pradeep said:   3 years ago
Here, L VALUE Error because only one time initialise b takes value 100, only for second-time b before need L VALUE.

Goku said:   2 months ago
L value = left value/left fun.
r value = right value.
((a >= 5) ? b = 100 : b) = 200;// Interpreted wrongly → Lvalue required.

So, it is declared that a=200 is not greatly important to understand and a= 100 is precedence.


Post your comments here:

Your comments will be displayed after verification.