c - Order of evaluation of arguments in function calling? -
i studying undefined behavior in c , came statement states
there no particular order of evaluation of function arguments
but standard calling conventions _cdecl
, _stdcall
, definition said (in book) arguments evaluated right left.
now confused these 2 definitions one, in accordance of ub, states different other in accordance of definition of calling convention. please justify two.
as graznarak's answer correctly points out, order in arguments evaluated distinct order in arguments passed.
an abi typically applies order in arguments passed, example registers used and/or order in argument values pushed onto stack.
what c standard says order of evaluation unspecified. example (remembering printf
returns int
result):
some_func(printf("first\n"), printf("second\n"));
the c standard says 2 messages printed in some order (evaluation not interleaved), explicitly not order chosen. can vary 1 call next, without violating c standard. evaluate first argument, evaluate second argument, push second argument's result onto stack, push first argument's result onto stack.
an abi might specify registers used pass 2 arguments, or on stack values pushed, entirely consistent requirements of c standard.
but if abi requires evaluation occur in specified order (so that, example, printing "second\n"
followed "first\n"
violate abi) still consistent c standard.
what c standard says the c standard itself not define order of evaluation. secondary standard still free so.
incidentally, not involve undefined behavior. there cases unspecified order of evaluation can lead undefined behavior, example:
printf("%d %d\n", i++, i++); /* undefined behavior! */
Comments
Post a Comment