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;
}
Answer: Option
Explanation:
Variable b is not assigned.
It should be like:
b = a >= 5 ? 100 : 200;
Discussion:
34 comments Page 2 of 4.
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;.
Balaji said:
9 years ago
Guys the precedence for ? : is just higher than assignment (=) hence compiler makes its grouping as below. To avoid confusion of precedence just remember one sentence compiler does its own grouping and starts execution from left to right.
(((((a >=5) ? b)=100): b)=200);
a>=5 results in constant 0 or 1. If 1 b is assigned and (<constant>)=100 remains to assign a value at Left side variable is required. Hence error Lvalue required.
(((((a >=5) ? b)=100): b)=200);
a>=5 results in constant 0 or 1. If 1 b is assigned and (<constant>)=100 remains to assign a value at Left side variable is required. Hence error Lvalue required.
Nikhil g said:
8 years ago
Why to use parentheses? How compiler will get to know end of expressions based on parentheses? Explain it clearly.
Pranali said:
8 years ago
@Gourav.
Because you are printing the value of b, so it will print 1.
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.
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.
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.
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;
}
int main()
{
int a = 10, b;
a >=5 ? (b=100): (b=200);
printf("%d\n", b);
return 0;
}
Prafull said:
1 decade ago
How the err "Error: L value required for b" comes? Can anyone explain for me?
Thanx in advance.
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.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers