Order of evaluation of arguments in function calling?

As Graznarak’s answer correctly points out, the order in which arguments are evaluated is distinct from the order in which arguments are passed.

An ABI typically applies only to the order in which arguments are passed, for example which registers are used and/or the order in which argument values are pushed onto the stack.

What the C standard says is that the order of evaluation is unspecified. For example (remembering that printf returns an int result):

some_func(printf("first\n"), printf("second\n"));

the C standard says that the two messages will be printed in some order (evaluation is not interleaved), but explicitly does not say which order is chosen. It can even vary from one call to the next, without violating the C standard. It could even evaluate the first argument, then evaluate the second argument, then push the second argument’s result onto the stack, then push the first argument’s result onto the stack.

An ABI might specify which registers are used to pass the two arguments, or exactly where on the stack the values are pushed, which is entirely consistent with the requirements of the C standard.

But even if an ABI actually requires the evaluation to occur in a specified order (so that, for example, printing "second\n" followed by "first\n" would violate the ABI) that would still be consistent with the C standard.

What the C standard says is that the C standard itself does not define the order of evaluation. Some secondary standard is still free to do so.

Incidentally, this does not by itself involve undefined behavior. There are cases where the unspecified order of evaluation can lead to undefined behavior, for example:

printf("%d %d\n", i++, i++); /* undefined behavior! */

Leave a Comment