why this program snippet display "8,7,7,8,-7,-8"
the program is:
main()
{
int i=8;
printf("%d\n%d\n%d\n%d\n%d\n%d\n",++i,--i,i++,i--,-i++,-i--);
}
Attempting to update an object more than once between sequence points
leads to undefined behavior, so *any* output is allowed.
Remember that i++ reads "evaluate to the current value of i, and
sometime before the next sequence point increment i by one."
Similarly, --i reads "evaluate to the current value of i - 1, and
sometime before the next sequence point decrement i by 1."
The "sometime before the next sequence point" part is the problem;
there is no explicit requirement on exactly *when* to apply the side
affect, as long as it occurs before the next sequence point. The
compiler implementor may choose to apply all side affects immediately
after each expression is evaluated, or defer them until just before the
sequence point, or some combination of the two depending on
circumstances.
So, in short, remember that any expressions of the following forms
invoke undefined behavior:
i = i++
a
= i++ or a[i++] = i
f(i++,i++)