Originally Posted by
Matticus
The OP seems to understand short circuit evaluation - it seems this thread is more of a question about operator precedence.
My impression is exactly opposite.
@euclid_mpls,
I wasn't assuming that you were familiar with assembly language. That would be a pretty stupid assumption. I just assumed you'd be able to look at it carefully with my comments and get something from it. But since you can't, here's a C interpretation of what the given code compiles to (basically just the assembly comments extracted). It shows exactly how short-circuit evaluation works and exactly why j and k are not incremented.
Here is the original code:
Code:
#include <stdio.h>
int main(void) {
int i = 1, j = 1, k = 1;
int n = ++i || (++j && ++k);
printf("%d: %d, %d, %d\n", n, i, j, k);
return 0;
}
Here is the interpretation:
Code:
#include <stdio.h>
int main(void) {
int n, i, j, k;
i = 1;
j = 1;
k = 1;
++i;
if (i != 0) goto SET_N_TO_1;
++j;
if (j == 0) goto SET_N_TO_0;
++k;
if (k == 0) goto SET_N_TO_0;
SET_N_TO_1:
n = 1;
goto PRINT_RESULTS;
SET_N_TO_0:
n = 0;
PRINT_RESULTS:
printf("%d: %d, %d, %d\n", n, i, j, k);
return 0;
}