K
Kevin Walzer
This code:
#include <stdio.h>
int main(void) {
int n1, n2; //two integers
n1 = 1;
n2 = 1;
printf("At first, n1 is %d, n2 is %d.\n", n1, n2);
n2 = n1++;
printf("After n2 = n1++, n1 is %d, n2 is %d.\n", n1, n2);
n2 = n1--;
printf("After n2 = n1--, n1 is %d, n2 is %d.\n\n", n1, n2);
return 0;
}
yields this output:
At first, n1 is 1, n2 is 1.
After n2 = n1++, n1 is 2, n2 is 1.
After n2 = n1--, n1 is 1, n2 is 2.
Question:
I would expect the statement:
n2 = n1++
to be equivalent to
n2=(n1 +1),
which, given that n1 = 1, should return a value of 2.
So why is n1's value 2 but n2's value is 1?
#include <stdio.h>
int main(void) {
int n1, n2; //two integers
n1 = 1;
n2 = 1;
printf("At first, n1 is %d, n2 is %d.\n", n1, n2);
n2 = n1++;
printf("After n2 = n1++, n1 is %d, n2 is %d.\n", n1, n2);
n2 = n1--;
printf("After n2 = n1--, n1 is %d, n2 is %d.\n\n", n1, n2);
return 0;
}
yields this output:
At first, n1 is 1, n2 is 1.
After n2 = n1++, n1 is 2, n2 is 1.
After n2 = n1--, n1 is 1, n2 is 2.
Question:
I would expect the statement:
n2 = n1++
to be equivalent to
n2=(n1 +1),
which, given that n1 = 1, should return a value of 2.
So why is n1's value 2 but n2's value is 1?