What will be the output of the following code?
What will be the output of the following code and WHY?
Code:
#include <stdio.h>
void main()
{
int x,y,z;
x=y=z=1;
z=++x||++y&&++z;
printf("x=%d y=%d z=%d\n",x,y,z);
}
Code:
#include <stdio.h>
void main()
{
float a=0.7
if (0.7>a)
printf("Hi");
else
printf("Hello");
}
I see no apparent mistake.
Perhaps the compiler doesn't allow the increment operator to be applied more than once on the same sentence that uses && and ||. Switching x and y results in y having a value of 2 and x of 1 (as opposed to x = 2 and y = 1) so only the first variable is actually incremented. if we use a fourth variable 'w' to receive the assignment of the operation, 'w' receives the value of 1 and z is not incremented (when we print all four variables). Apparently incrementing more than one variable is not even recognized by the compiler. Then again, that's just my educated guess.
Compiler doesn't check if it has got the answer.
Quote:
Originally Posted by jalnewbie
Perhaps the compiler doesn't allow the increment operator to be applied more than once on the same sentence that uses && and ||. Switching x and y results in y having a value of 2 and x of 1 (as opposed to x = 2 and y = 1) so only the first variable is actually incremented. if we use a fourth variable 'w' to receive the assignment of the operation, 'w' receives the value of 1 and z is not incremented (when we print all four variables). Apparently incrementing more than one variable is not even recognized by the compiler. Then again, that's just my educated guess.
If a left part of the || is +ve, it is obvious that the output will be 1 therfore compiler doesnot calculate the other side of the ||. It ignores it.
Similarly if left side of && is 0 the result is obviously 0 therfore compiler discards the other part.