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;
}
Discussion:
22 comments Page 1 of 3.
Debashish Ghosh said:
1 decade ago
When the compiler reaches the second n in
y == 1 ? n = 0 : n = 1;
It is convinced that the conditional expression is completely scanned. This expression evaluates to an R-value after which the compiler encounters an assignment operator (=). So essentially the compiler sees the above statement as
(y == 1 ? n = 0 : n) = 1;.
And the expression inside the brackets evaluates to an R-value. Since we can not place an R-value before the assignment operator (=), the compiler generates the error message "L-value required".
y == 1 ? n = 0 : n = 1;
It is convinced that the conditional expression is completely scanned. This expression evaluates to an R-value after which the compiler encounters an assignment operator (=). So essentially the compiler sees the above statement as
(y == 1 ? n = 0 : n) = 1;.
And the expression inside the brackets evaluates to an R-value. Since we can not place an R-value before the assignment operator (=), the compiler generates the error message "L-value required".
(1)
Ravi said:
1 decade ago
#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;
}
Compile and run the following code in Turbo C or in GCC.
The conditional operator ?: has following form.
(condition)?true part : false part.
Thus, the value of n will be 0 not 1.
int main()
{
int n = 0, y = 1;
y == 1 ? n=0 : n=1;
if(n)
printf("Yes\n");
else
printf("No\n");
return 0;
}
Compile and run the following code in Turbo C or in GCC.
The conditional operator ?: has following form.
(condition)?true part : false part.
Thus, the value of n will be 0 not 1.
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)...
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.
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 .
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 .
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.
((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.
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.
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.
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)
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)
Suraj said:
10 years ago
Yes, @Sandya is right when run these code in GCC it also gives "NO" as a output and if you assign n=1 in if than it gives "YES" as a output so before If there is no bug.
Praween kumar said:
7 years ago
y==1?n=0:n=1; <=> (y==1?n=0:n)=1;
Here, within parenthesis is an expression so, we can't assign value in an expression.
Here, within parenthesis is an expression so, we can't assign value in an expression.
(1)
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers