Possible duplicate:confused about printf (), which contains the prefix and postfix operators.
I came across a code with the following snippet,
int main() { int c = 100; printf("\n %d \t %d \n", c, c++); return 0; }
I expected the output to be 100 and 101, but I get the output as
101 100
Can someone help me find out why?
Standards C and C ++ do not guarantee the procedure for evaluating function parameters. Most compilers will evaluate parameters from right to left, because this is the order they get on the stack using the cdecl calling convention.
There is no guarantee whether it will be judged first con the left or c++on the right.
c
c++
Unspecifeid , , Undefined .
1.9 ++:
" unspecified (for example, order of evaluation of arguments to a function). , ".
unspecified
for example, order of evaluation of arguments to a function
printf ("%d\n", c++) printf ("%d\n", c), 100 . c, ++ , , - undefined .
printf ("%d\n", c++)
printf ("%d\n", c)
printf works from right to left, so the first C ++ (c = 100) is executed, after C ++ and C = 101 are executed, therefore the output is 101 and 100 http://en.wikipedia.org/wiki/Printf