When you wrote
a + sum(a - 1)
, a rough translation of that C code into English is:
"Take a
plus the result of the sum
function applied to the quantity a-1
."
That makes sense, and it's just what you want.
If, on the other hand, you write
a + sum(--a)
, now what you're saying is,
"Take a
plus the result of the sum
function applied to, hang on a minute, I want to take the variable a
minus 1 and assign it back to a
and then, where was I, pass it to sum()
."
Is that really what you want to do? Why would you want to modify a
's value in the middle of calling sum()
? You wouldn't want to do that, you don't need to do that, that has nothing to do with the problem of calling sum(a - 1)
. So don't write it that way. It's confusing to you and me, and what's worse, it's so confusing that the compiler can't figure it out, either. It's so confusing that it's undefined behavior, meaning that the compiler isn't even required to figure out what you mean, and the compiler is explicitly allowed to fall back and punt, and compile your program into something that doesn't work — which is precisely what you discovered when you tried it.
See the canonical SO question Why are these constructs using pre and post-increment undefined behavior? for much (much!) more on undefined expressions like these.