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.

Chethan N said:   8 years ago
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.

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

Manjuanath (mj) said:   1 decade ago
In grouping is required otherwise error as Lvalue required.

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

Here you will not get error friends.
(1)

Rajesh katta said:   6 years ago
Here the ternary operator is used so that it will need the left-hand side value like in the form of ternary syntax.

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++;.

Amit said:   8 years ago
The syntax y == 1 ? n=0 : n=1; should be written as y==1?0:1;.

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

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

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

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

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


Post your comments here:

Your comments will be displayed after verification.