Consider the following:
Code #1
#include <stdio.h>
int main() {
int a=1, b=2, c=3, n;
n = (( ++ a ) * b ) - ( c -- );
printf("a = %d, b = %d and c = %d\n", a, b, c);
printf("with n = %d", n);
return 0;
}
The output for Code #1 is:
a = 2, b = 2, c = 2
with n = 1
I am trying to figure out exactly how this code is read and performed by the computer.
Now, consider an earlier example that I attempted.
Code #2
#include <stdio.h>
int main() {
int i = 3, j = 8, k;
k = j +++ i;
k /= 2;
printf(%d %d %d", j, i, k);
return 0;
}
the output for Code #2 is:
9 3 5
From this I concluded that the j++ operation takes effect after the line statement k = j +++ i;
This is because if j became 9 during the statement, then the final value of k would be 6.
However this does not seem to match the output of Code #1, as if ++a and c-- did not take effect on the number until after the line, then the value of n would be:
(( 1 ) * 2 ) - ( 3 ) = -1
Furthermore, of both took effect during the line statement, then the value of n would be:
(( 2) * 2 ) - ( 2) = 2
I must be interpreting this wrong. Could someone please help me?



LinkBack URL
About LinkBacks


