int main()
{
int x, y, z, q;
x = y = z = 1;
q = ++x || ++y && ++z; PRINT3 (x, y, z, q);
/* x=2 y=1 z=1 q=1*/
can someone explain to me whats going on here, i dont understand the logic and why some variables are being increamented and others are not
int main()
{
int x, y, z, q;
x = y = z = 1;
q = ++x || ++y && ++z; PRINT3 (x, y, z, q);
/* x=2 y=1 z=1 q=1*/
can someone explain to me whats going on here, i dont understand the logic and why some variables are being increamented and others are not
Good lord... If I ever write code like that please shoot me!
First off the logic operators are all wrong.
| is binary OR as in 1 | 0 = 1
|| is logical or as in this or that.
The same with the & ...
& is binary AND as in 1 & 1 = 1
&& is logical and as in this and that.
PRINT3 is not a C function so it must be defined someplace.
++ x | ++y & ++z would amount to 2 OR 2 AND 2 ... which has no choice but to equal 2
The code, as posted, makes no sense to me at all.
i got this in a test
now you know how i feel about this i totally flunked the test apparently the commented values are the results of the logical operations
#define PRINT3(x,y,z,q) printf("x=%d\ty=%d\tz=%d\tq=%d\n",x,y,z,q)
I almost fainted when I saw your code. I waited for someone else to comment
first, fearing that maybe I'm drunk or off my rocker.
My only input is this:
main should be declared an int, and return an int upon successful
completion:
Code:int main(void) { code here return 0; }
ok let me put the whole test
inserthell on earthCode:#include <stdio.h> #define PRINT3(x,y,z,q) printf("x=%d\ty=%d\tz=%d\tq=%d\n",x,y,z,q) int main() { int x, y, z, q; x = y = z = 1; q = ++x || ++y && ++z; PRINT3 (x, y, z, q); /* x=2 y=1 z=1 q=1 since the expression is equivalent to ++x || (++y && ++z); */ x = y = z = 1; q = ++x && ++y || ++z; PRINT3 (x, y, z, q); /* x=2 y=2 z=1 q=1 since the expression is equivalent to (++x && ++y) || ++z; */ x = y = z = 1; q = ++x && ++y && ++z; PRINT3 (x, y, z, q); /* x=2 y=2 z=2 q=1 */ x = y = z = 1; q = ++x || ++y || ++z; PRINT3 (x, y, z, q); /* x=2 y=1 y=1 q=1 */ x = y = z = 0; q = ++x && ++y || ++z; PRINT3 (x, y, z, q); /* x=1 y=1 z=0 q=1 */ x = y = z = 0; q = ++x || ++y && ++z; PRINT3 (x, y, z, q); /* x=1 y=0 z=0 q=1 */ x = y = z = 0; q = ++x && ++y && ++z; PRINT3 (x, y, z, q); /* x=1 y=1 z=1 q=1 */ x = y = z = 0; q = ++x || ++y || ++z; PRINT3 (x, y, z, q); /* x=1 y=0 z=0 q=1 */ x = y = z = -1; q = ++x && ++y || ++z; PRINT3 (x, y, z, q); /* x=0 y=-1 z=0 q=0 */ x = y = z = -1; q = ++x || ++y && ++z; PRINT3 (x, y, z, q); /* x=0 y=0 z=-1 q=0 */ x = y = z = -1; q = ++x && ++y && ++z; PRINT3 (x, y, z, q); /* x=0 y=-1 z=-1 q=0 */ x = y = z = -1; q = ++x || ++y || ++z; PRINT3 (x, y, z, q); /* x=0 y=0 z=0 q=0 */ return 0; }
This could be a part of the problem Sequence point - Wikipedia, the free encyclopedia
Among other things, this question tests whether you understand the short-circuit behaviour of || and &&. Now, ++x evaluates to 2, which evaluates to true, hence the left hand side of the || evaluates to true. Since this is sufficient to determine the result of the entire || expression, evaluation stops here, hence y and z are not incremented. Furthermore, the result of the expression is 1 (i.e., the "canonical" true value), hence 1 is assigned to q.Originally Posted by ashfire
No, if the bitwise operators were used, there would not be this short-circuit/lazy evaluation.Originally Posted by CommonTater
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
laserlight... If you understand this, how about picking a couple of lines and giving me a walk through... I have to admit this one just baffles me...
I already went through ashfire's original questionOriginally Posted by CommonTater
If you want another one, examine:
Now, ++x evaluates to 0, but this is not enough to determine the result of the || expression, so ++y && ++z is evaluated. ++y evaluates to 0, and this is enough to determine that the result of the && expression is 0, so evaluation stops here, meaning that ++z is not evaluated (thus z remains as -1). The result of the expression is 0, so 0 is assigned to q. This is how we get:Code:x = y = z = -1; q = ++x || ++y && ++z; PRINT3 (x, y, z, q);
Code:/* x=0 y=0 z=-1 q=0 */
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)