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.

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)

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)

Prabhanjan mishra said:   10 years ago
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.
(1)

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).

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

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.

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.

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

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?

Naveen said:   10 years ago
Explain this logic:

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


Post your comments here:

Your comments will be displayed after verification.