C Programming - Declarations and Initializations - Discussion

Discussion Forum : Declarations and Initializations - Find Output of Program (Q.No. 7)
7.
What is the output of the program
#include<stdio.h>
int main()
{
    int x = 10, y = 20, z = 5, i;
    i = x < y < z;
    printf("%d\n", i);
    return 0;
}
0
1
Error
None of these
Answer: Option
Explanation:
Since x < y turns to be TRUE it is replaced by 1. Then 1 < z is compared and to be TRUE. The 1 is assigned to i.
Discussion:
37 comments Page 4 of 4.

Krishna said:   1 decade ago
Why it is 10 not 20 or 30?

Xyz said:   1 decade ago
Thanks wikiok your explanation helped me to understand the answer.

Palak agrawal said:   1 decade ago
Yaa anyone tell the reason in above program. Why it is 10 not 20 or 30?

Shivaji said:   1 decade ago
i=x,y,z;
Because it is
Just like below..
i=x;
y;
z;

Sai said:   1 decade ago
int main()
{
int x = 30, y = 20, z = 5, i;
i = x <y <z;
printf("%d\n", i);
return 0;
}

Please explain this program.

Deepak said:   1 decade ago
@Sai.
i=((30<20)<5); //in this (30<20) is false so it is assigned 0.
i=(0<5); //in this (0<5) is true so it is assigned 1.
i=1;

False=0, True=1

Vidya said:   1 decade ago
@Krishna,as per priority given to the operaters, '=' is having highest priority and the operator ' will move from left to right.

and given as i=x,y,z.
so,
i=x=10.


Post your comments here:

Your comments will be displayed after verification.