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
--a
, so it can either decrement
a
before or after the addition. The only thing you know for sure is that following the assignment to
b
,
a
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
int a=10;
printf("%d, %d\n", a, --a)
might print "10, 9", or it might print "9, 9"