As surprising as it might be, the behaviour of the expression
n + calculateSum(--n);
is undefined, because --n
not only evaluates to the value of n
after being decremented by 1, it also changes n
to the new value. The change (a side effect), however, is unsequenced in relation to the other evaluation of n
(a value computation) on the left. And according to C11 Appendix J.2., the behaviour is undefined, when
A side effect on a scalar object is unsequenced relative to either a different side effect on the same scalar object or a value computation using the value of the same scalar object (6.5).
More of the same class of errors in this question.
You can consider yourself lucky when you got a wrong value, because your compiler could have also generated code that would return the "correct value"... given the phase of the moon, and the optimization settings, or the number of lines in the calling function...