What should be the output of this C program?
int main()
{
int x=1,y=1,z;
if (z=(y>0)) x=5;
printf("%d %d",x,z);
return 0;
}
As expected, the output is X is 5 and Z is 1. This is because when expression y>0 is evaluated it is true and so on and so forth. Now the problem is in this program:
int main()
{
int x,y;
for (y=1;(x=y)<10;y++)
;
printf("%d %d",x,y);
return 0;
}
should not the output be an infinite loop? Reason being, (x=y) will always return true(1), which is always less than 10?
No,
(x=y)returns the new value after setting x to y’s value.However,
(x==y)returns 1 if they are equal, and 0 if not.