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:
33 comments Page 1 of 4.

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

Rishikesh Sonawane said:   3 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.

Pranali said:   7 years ago
@Gourav.

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

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.

Balaji said:   8 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.

Barige Rajesh said:   8 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)

Shivaprakash a said:   9 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).

Naveen said:   9 years ago
Explain this logic:

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

Prabhanjan mishra said:   9 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)

Gourav said:   9 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)


Post your comments here:

Your comments will be displayed after verification.