This has to do with sequence points
. I believe that both 20 and 21 are valid answers, since the compiler is free to choose when to evaluate
, so it can either decrement
before or after the addition. The only thing you know for sure is that following the assignment to
will have the value 20.
You should avoid expressions that access and use the increment/decrement operators on the same variable at the same time in different locations. This includes passing the variable to functions. For example
printf("%d, %d\n", a, --a)
might print "10, 9", or it might print "9, 9"