Possible Duplicate:
Parameter evaluation order before a function calling in C
For the below code I expected the output to be 20 and 76 but instead 75 and 21 is comming as output .Please explain why is so.
#include<stdio.h>
unsigned func(unsigned n)
{
unsigned int a =1 ;
static unsigned int b=2;
a+=b; b+=a;
{
unsigned int a=3;
a+=b; b+=a;
}
//printf("%d %d ",a,b);
return (n+a+b);
}
int main()
{
printf("%d %d\n",func(4),func(5));
return 0;
}
you are expecting func(4)
to be called before func(5)
but the opposite happens with your compiler. The order of evaluation of function parameters is unspecified by C standard. So, compiler is free choose which function to call first. So across different runs you may observe different order of function calls, though it's very unlikely to happen that way with the same compiler.
The order of evaluation of func(4) and func(5) isn't defined by the C standard(s).
The order of evaluation of expression is unspecified behaviour
hence func(4)
and func(5)
may be called in different order as you supposed to
You might like to visit it for more
Compilers and argument order of evaluation in C++
Parameter evaluation order before a function calling in C
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With