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

Barige Rajesh said:   9 years ago
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.
(1)

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.

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.

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.

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

Shivaprakash a said:   10 years ago
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).

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?

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.

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.

Aloke 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;
}
gives the output 100 on the online compilar which is provided by this site
check it my friends


Post your comments here:

Your comments will be displayed after verification.