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 2 of 3.

Navneeraj Mishra said:   1 decade ago
In turbo c this prog gives error: "L-value required".

While in dev c++ its executes successfully and gives the o/p as: No

Ravindra Nath Mishra said:   2 decades ago
LVALUE required means lift hand side value this error occurs when there is variable declared to store the returned value.
In following question y==1 is condition which is true but there is left hand side variable in order to store the returned value thus, lvalue error occurs .

Rohini said:   1 decade ago
Problem : y==1? n=0:n=1;

Soln: There should be variable on a left-side of assignment operator to hold than it will give proper result.

int x;
x=(y==1?n=0:n=1);
where x = 1(true value returned)

Ashok said:   1 decade ago
Hi friends....
y == 1 ? n=0 : n=1;
in the above statement we have 2 assignment statements after condition.
but the 2nd assignment statement should be in ( ).
because if there are two assignment statements after condition in ternary statement the second statement should be in ( )..
y == 1 ? n=0 : (n=1);
(according to test your c skills by Y.KANETHKAR)...

Sathuragiri said:   1 decade ago
n=(y==1)?0:1 means check if y=1 or not. then y=1 means print n=0 or print n=1. Thus the way depends upon the y value, the given value need to store some where. In c lang, we assigned left hand side at which the given value will store autometically. Thus the way n is missing in given program.

Santhi said:   1 decade ago
What do you mean by ((y == 1) ? n) = 0 : n = 1;

Deepa said:   1 decade ago
There is no error in program, but value of n is being 1, why;.

Abhijit said:   1 decade ago
I dont think there is any error in the prog.

Kiran said:   1 decade ago
There is an error in ?: ternary operator .

Here, y ==1 ? n =0 :n =1 evaluates to y==1 is true then it goes to n = 1 and assigned for it But n is not assigned to any of the variable. It gives an error which is Lvalue is required.

Sandy said:   2 decades ago
I don't think there is an error in the program. It give No as output when executed on Dev C++;.


Post your comments here:

Your comments will be displayed after verification.