This has nothing to do with XCode. This is just C syntax. There is a prefix increment and a postfix increment.
Both alter their operand, but they evaluate to different things. The prefix operator evaluates to the value of the variable after incrementing. The postfix operator evaluates to the value of the variable before incrementing.
I don't know if using functions will be clearer to you now or not, but I'll give it a try.
Code:
int prefixIncrement(int *x) {
*x = *x +1;
return *x;
}
int postfixIncrement(int *x) {
int temp = *x;
*x = *x +1;
return temp;
}
int main(int argc, char *argv[]) {
int a,b,c;
a = 5;
b = prefixIncrement(&a);
printf("a is: %d, b is: %d\n",a,b);
a = 5;
c = postfixIncrement(&a);
printf("a is: %d, c is: %d\n",a,c);
return 0;
}
The output is:
a is 6, b is 6
a is 6, c is 5
prefixIncrement(&a) is the same as ++a.
postfixIncrement(&a) is the same as a++.
Every expression has some sort of value in C (though sometimes it is void... which means it doesn't... there are always exceptions). Even things that don't seem like they should:
Code:
int main(int argc, char *argv[]) {
int a,b,c,d;
a = 5;
b = 7;
c = 0;
d = 0;
d = c = b + a;
printf("a is: %d, b is: %d, c is: %d, d is %d\n",a,b,c,d);
return 0;
}
Output:
a is: 5, b is: 7, c is: 12, d is: 12
The expression:
evaluates to the left-hand operand of the = after the assignment has occurred. This leads to the common error:
Code:
if(a = b) {
...
} else {
...
}
In this case, the condition is evaluating the value of a after b is assigned to it. This means that if b is any value other than 0, the conditional is true, no matter what a is. This is rarely, if ever, the real intention.
I hope this helps to some degree. I am afraid if you are just getting started programming this may have muddled things further, and if so, my apologies (and try to forget it completely).
-Lee
P.S. Try this on for size:
Code:
int a,b,c;
a = 5
b = 7;
c = a+++++b;
=) It amuses me that you can do that and it's valid. To the OP: What are the values for a, b, and c when this finishes?