C Programming - Control Instructions - Discussion

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;
}

[A]. 100
[B]. 200
[C]. Error: L value required for b
[D]. Garbage value

Answer: Option C

Explanation:

Variable b is not assigned.

It should be like:

b = a >= 5 ? 100 : 200;


Sandy said: (Sep 1, 2010)  
It gives output as 100 when executed on DEV C++.

Prafull said: (Sep 2, 2010)  
How the err "Error: L value required for b" comes? Can anyone explain for me?

Thanx in advance.

Vishal Yadav said: (Nov 21, 2010)  
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: (Dec 11, 2010)  
Still I don't understood.

Vadya said: (Jan 23, 2011)  
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: (Mar 14, 2011)  
L value means left side of the expression is equal to b.so
b=(a>=5)?b=100:b=200;

Durga said: (Mar 31, 2011)  
Hi this program explanation is not clear. Please send brief expl.

Mani said: (Jun 1, 2011)  
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: (Jun 6, 2011)  
Mani is correct.

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

Aloke said: (Aug 28, 2011)  
#include<stdio.h>
int main()
{
int a = 10, b;
a >=5 ? (b=100): (b=200);
printf("%d\n", b);
return 0;
}
gives the output 100 on the online compilar which is provided by this site
check it my friends

Praveen said: (Aug 28, 2011)  
Aloke would you please explain your program ? How does it work ?

Praveen said: (Aug 28, 2011)  
@Mani

The program which Aloke has posted works even though the expression a >=5 ? (b=100): (b=200); is not assigned to any value .. Would you please explain us in detail if you are clear with the concept ?

Maikal Kumar said: (Dec 20, 2011)  
Answer must be 100; because a=10 thats are >a. But please help me about lvalue value.

Sarang said: (Apr 17, 2012)  
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: (Jun 24, 2012)  
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: (Dec 18, 2012)  
What is the meaning of ? in between the a>=5 and b=100.

Ajeet said: (Apr 11, 2013)  
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;.

Priyanka K. said: (Jul 31, 2013)  
Condition is satisfying, hence b=100 will be assigned to b and it becomes print 100?

Parveen Soni said: (Aug 4, 2013)  
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.

Prajyot said: (Aug 28, 2013)  
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.

Nitesh said: (Nov 11, 2014)  
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

Abcd said: (Aug 18, 2015)  
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?

Gourav said: (Aug 27, 2015)  
#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.

Prabhanjan Mishra said: (Nov 5, 2015)  
I have compiled on the same code in Microsoft Visual C++ then the output will be 100 because the logic behind this is.

If b>5; then b=100.

Otherwise if b should be 200.

(condition) ? true : false.

Naveen said: (Nov 16, 2015)  
Explain this logic:

c = 0.
b = a%10;
c = c+b;
a = a/10;

Shivaprakash A said: (Jan 26, 2016)  
Hey guys I compiled and executed in linux ubuntu with terminal.

Got 'lvalue' error initially with a >= 5 ? b = 100: 200; as given in question.

Tried b = a >= 5 ? b = 100: b = 200; also gave same error.

Hence no variable required. Just use parenthesis works for me. Also thanking @Parveen soni.

a >= 5 ? (b = 100) : (b = 200).

Barige Rajesh said: (Apr 29, 2016)  
Hii Friends.

Syntax of the ternary operator

(exp1)?(exp2):(exp3);

a>=5?(b=100):(b=200);
for the above statement compiler easily identifies all the three expressions of ternary operator. (i.e) exp1=(a>=5?) exp2=(b=100) exp3=(b=200)

But for the below statement,

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

The compiler could not identify from where to where the third expression is?

However, the compiler can understand that before to "?" is the expression1 and between "?" and ":" is expression2.

The compiler could not understand from where to where the expression3 is???? because the ternary operator is right to left the association.

You might think that between ":" to ";" why can't a compiler identify it as an expression3.

the semicolon is only an end of the statement indicator.

So, now if you give statement as follows,
a>=5?b=100:(b=200);

now compiler can understand that (b=200) as an expression3. so it, not an error.

Balaji said: (Oct 21, 2016)  
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.

Nikhil G said: (Feb 28, 2017)  
Why to use parentheses? How compiler will get to know end of expressions based on parentheses? Explain it clearly.

Pranali said: (Apr 1, 2017)  
@Gourav.

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

Rishikesh Sonawane said: (Mar 19, 2021)  
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.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.