I was trying to understand the cdecl
calling convention and I stumbled on this weird behaviour.
As per cdecl
standards, the calling function stores the parameters from Right to Left on to the stack and then calls the target function. So I assumed the parameters would be evaluated left to right since that would resemble a stack flow or it could still evaluate from right to left given the cdecl
convention for storing the parameters.
But the output of the below program has baffled me. Below is a simple program I wrote to understand the evaluation of parameters.
void fun (int a, int b, int c) {
printf("a: %d, b: %d, c: %d\n", a, b, c);
}
int main()
{
int i = 2;
fun(i, i++, i);
}
Expected Output: a: 3, b: 2, c: 2 or a: 2, b: 2, c: 3
Actual Output: a: 3, b: 2, c: 3
gcc --version
gcc (Ubuntu 4.8.2-19ubuntu1) 4.8.2
Copyright (C) 2013 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Can someone throw some light on this?