As far as I can tell, a cdecl
function pushes its parameters onto stack from right to left. However,
int n = 0;
printf("%d %d", n=8, n); // Output: 8 8
I expect it to output 8 0
, as I thought it calculates parameters by the order by which it pushes parameters--from right to left . I tried the code on TDM-GCC 32bit/64bit
, GCC x64
, MSVC x86/x64
and clang x64
, and got the same result.
So I concluded that a cdecl
function calculates its parameters before pushing them onto stack, and the two processes proceed in reverse order.
However,
int n = 0;
printf("%d %d", n=8, n=0); // Output: 8 8
I do NOT understand it.
The order of evaluation is not defined in C language.
Your example prints
8 8
most likely because the compiler has optimized out the assignment and it could do it as it is UB.The following example will show different behaviour if this optimization cannot be done (and it is up to the compiler as it is UB) .
https://godbolt.org/z/crKbs1hjE