C Programming - Control Instructions - Discussion


Which of the following statements are correct about the below program?

int main()
    int n = 0, y = 1;
    y == 1 ? n=0 : n=1;
    return 0;

[A]. Error: Declaration terminated incorrectly
[B]. Error: Syntax error
[C]. Error: Lvalue required
[D]. None of above

Answer: Option C


No answer description available for this question.

Ravindra Nath Mishra said: (Jun 2, 2010)  
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 .

Ramesh said: (Jun 26, 2010)  
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.

Venkz said: (Aug 28, 2010)  
y == 1 ? n=0 : n=1; should be replaced by


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

Kiran said: (Jan 3, 2011)  
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.

Abhijit said: (Jan 3, 2011)  
I dont think there is any error in the prog.

Deepa said: (Apr 10, 2011)  
There is no error in program, but value of n is being 1, why;.

Santhi said: (Jul 13, 2011)  
What do you mean by ((y == 1) ? n) = 0 : n = 1;

Sathuragiri said: (Oct 27, 2011)  
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.

Ashok said: (Nov 21, 2011)  
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)...

Rohini said: (Dec 8, 2011)  
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;
where x = 1(true value returned)

Sanjana said: (May 21, 2012)  
What do you mean by ((y==1)?n)=0:n=1;?

Debashish Ghosh said: (Jun 1, 2012)  
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".

Navneeraj Mishra said: (Jul 23, 2012)  
In turbo c this prog gives error: "L-value required".

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

Ashok said: (Sep 17, 2012)  
Thank you kiran.

Ravi said: (May 24, 2014)  
int main()
int n = 0, y = 1;
y == 1 ? n=0 : n=1;
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.

Manjuanath (mj) said: (Jul 12, 2015)  
In grouping is required otherwise error as Lvalue required.

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

Here you will not get error friends.

Suraj said: (Oct 20, 2015)  
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.

Amit said: (Jun 9, 2017)  
The syntax y == 1 ? n=0 : n=1; should be written as y==1?0:1;.

Chethan N said: (Dec 30, 2017)  
Expression? True(1):false(0)
In false condition it's always value is 0.
We don't modify it,

If can't assign the value n=1 in false condition.

Praween Kumar said: (Aug 4, 2018)  
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.

Rajesh Katta said: (Mar 6, 2020)  
Here the ternary operator is used so that it will need the left-hand side value like in the form of ternary syntax.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.