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 1 of 4.

Naveen said:   1 decade ago
#include<stdio.h>
int main()
{
int x = 10, y = 20, z = 5, i;
i = x < y < z;
printf("%d\n", i);
return 0;
}

step 1: In the above code < is having highest priority and = is having priority next to <

step 2: < symbol have left associate,so it will finish all the things with <

step 3: = is having next priority so values get into i

i=1.

Naveen said:   1 decade ago
#include<stdio.h>
int main()
{
int x = 10, y = 20, z = 5, i;
i = x,y,z;
printf("%d\n", i);
return 0;
}

If you try the above code

step 1: = is having highest priority than ,

step2: i=10 will be initalized

step3: , operater will travell form left to right

So the out put will be 10.

Indranil Roy said:   1 decade ago
I am explaining my thought.

#include<stdio.h>
int main()
{
int x = 10, y = 20, z = 5, i;
i = x < y < z;
printf("%d
", i);
return 0;
}
here i=10<20<5
Now c compiler starts execution from left side.
that is 10<20 returns TRUE and TRUE < 5 means 0 < 5 which is TRUE means Ans is 1

Gangadhararao said:   1 decade ago
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;
}

Gow is the ouput becomes 1 ?

Niharika said:   1 decade ago
I think #include<stdbool.h> Header file must be used to assign 1=true and 0=false but it is not used in the program.

How is it possible? to get output as 1 When we compare x<y.anyone explain?

Loelynk said:   2 years ago
Hello guys,

10<20(absolutely this condition is true.
True are 1
False are 0
The first x<1
Y are 1(true), that's time 1<5.
The second condition is also true(1), that 1 is passing through i.
(2)

Kumar Ajit said:   10 years ago
#include<stdio.h>
int main()
{
int x=10,y=20,z=5,i;
i=y>z>x;
printf("%d\n",i);
return 0;
}

Why out put is 0? I not understand friend explain it.

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.

Shiva said:   1 decade ago
Can anyone solve this one?

int main()
{
int x = 10, y = 20, z = 5, i;
i = x >y > z;
printf("%d\n", i);
return 0;
}


Post your comments here:

Your comments will be displayed after verification.