/* x++ */
inline int postincrement(int *x) {
int temp = *x;
*x = *x + 1;
return temp;
}
/* ++x */
inline int preincrement(int *x) {
*x = *x + 1;
return *x;
}
But in typical usage, where the expression value is not used, it doesn't make any difference: for (int i = 0; i < 10; ++i) {
If one looks at the assembly, it's clearly equivalent to: for (int i = 0; i < 10; i++) {
Incidently, the latter seems more readable.Compilers are sometimes smart enough to remove the unnecessary operation in C++ (e.g. switch to ++x themselves), but I always use ++i for the same reason people simplify their usage of C in between sequence points --- it's an easy transformation, and why tempt fate?
To her credit, she didn't penalize me for disagreeing with her and I still got the offer.
will give "0 0 1" (gcc 4.2.1), the increment "shouldn't" happen until after the ; if you're going with post statement. I think your rule would expect "0 0 0" with a being 2 after the printf.
BUT! you get a warning, so that's nice.
int a = 0, b, c;
b = a++, c = a++;
But, there is no defined order in which to evaluate function arguments."0 0 1" is what I would expect from your code, and identifier a should end up as 2.
Classic example: signed integer overflow. It worked for decades. Then one day it didn't.
If you want to know about this particular example: https://news.ycombinator.com/item?id=6824514 (not personally confirmed)
Compiler users generally want something that "just works" and doesn't do anything unexpected, but in the case of a low-level language like C, doing away with undefined behavior essentially would mean pessimistically avoiding many optimisations on the 99%+ of straightforward, reasonable code out there in favor of not doing anything surprising on the remaining fraction of dubious code that depends on certain things happening in scenarios where behavior is undefined according to the C standard. There are languages that make that choice, but C isn't one of them.
I have fixed feelings about the integer overflow issue because it's so easy to trigger, unlike triple post increment fake examples. And it usually results in a security problem. For very little benefit, IMO.
Clang says:
zsh% clang -o sequencepoints sequencepoints.c
sequencepoints.c:7:18: warning: multiple unsequenced modifications to 'i' [-Wunsequenced]
int r = 1 * a[i++] + 2 * a[i++] + 3 * a[i++];
^ ~~
1 warning generated.
And prints: zsh% ./sequencepoints
140