Printf using stack?

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?

+5
source share
4 answers

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.

+9
source

There is no guarantee whether it will be judged first con the left or c++on the right.

Unspecifeid , , Undefined .

1.9 ++:

" unspecified (for example, order of evaluation of arguments to a function). , ".

+5

printf ("%d\n", c++) printf ("%d\n", c), 100 . c, ++ , , - undefined .

+1

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

-3
source

All Articles