What would be the output of this program ?
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int x=20,y=30,z=10;
int i=x<y<z;
printf("%d",i);
getch();
}
Actually i=20<30<10, so the condition is false and the value of i should be 0 but i equals 1. Why?
This
int i=x<y<z;doesn’t work the way you intended.The effect is
int i=(x<y)<z;, wherex<yis evaluated first, and the valuetrueis then compared toz.Pascal points out below that in C the result of the comparison is
1instead oftrue. However, the C++trueis implicitly converted to1in the next comparison, so the result is the same.