C Programming - Control Instructions - Discussion

Discussion Forum : Control Instructions - Point Out Correct Statements (Q.No. 8)
8.
Which of the following statements are correct about the below program?
#include<stdio.h>
int main()
{
    int n = 0, y = 1;
    y == 1 ? n=0 : n=1;
    if(n)
        printf("Yes\n");
    else
        printf("No\n");
    return 0;
}
Error: Declaration terminated incorrectly
Error: Syntax error
Error: Lvalue required
None of above
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
22 comments Page 3 of 3.

Venkz said:   2 decades ago
y == 1 ? n=0 : n=1; should be replaced by

n=(y==1)?0:1;

Ramesh said:   2 decades ago
y == 1?n=0:n=1; this statement evaluates like following
((y == 1) ? n) = 0 : n = 1;

== has higher precedence then ? : operator then assignment operator.
(y == 1) ? n evaluates to rvalue not lvalue, so the assignment operator doesn't work.

Hence lvalue is required.


Post your comments here:

Your comments will be displayed after verification.