A
Anthony Paul
Hello everyone,
I have a snippet of C code (found at the bottom of this post) that is
puzzling me, and I get different results on different compilers (which
is also disturbing).
On my visual studio 2008 c++ compiler and on gcc, I get the following
values :
a = 1308, b = 665, c = 665, d = 665
When I saw these values I was shocked! c and d have no business being
what they are, and I'm hoping someone can explain why.
On tanenbaum's compiler, I get what's expected :
a = 1308, b = 665, c = 666, d = -1
After some experimenting, a friend and I found that if you wrap the
expressions in printf statements (eg. func(printf("%d", 1973-z),
etc...)) gcc would change its tune and output what's expected (the
same as tanenbaum's compiler); visual studio on the other hand
continues to output the same nonsense.
So my question (as a C compiler writer who wants to implement this
correctly) to you C gurus is... is this behavior defined and if so,
which compiler is correct and why the discrepencies between these
compilers?
Regards,
Anthony
void func(int a, int b, int c, int d)
{
// what are the values of a, b, c, and d at this point (assuming
parameters are parsed right to left) ??
}
void main()
{
int z = 1973;
func(1973-z, --z, z=666, z=-1);
}
I have a snippet of C code (found at the bottom of this post) that is
puzzling me, and I get different results on different compilers (which
is also disturbing).
On my visual studio 2008 c++ compiler and on gcc, I get the following
values :
a = 1308, b = 665, c = 665, d = 665
When I saw these values I was shocked! c and d have no business being
what they are, and I'm hoping someone can explain why.
On tanenbaum's compiler, I get what's expected :
a = 1308, b = 665, c = 666, d = -1
After some experimenting, a friend and I found that if you wrap the
expressions in printf statements (eg. func(printf("%d", 1973-z),
etc...)) gcc would change its tune and output what's expected (the
same as tanenbaum's compiler); visual studio on the other hand
continues to output the same nonsense.
So my question (as a C compiler writer who wants to implement this
correctly) to you C gurus is... is this behavior defined and if so,
which compiler is correct and why the discrepencies between these
compilers?
Regards,
Anthony
void func(int a, int b, int c, int d)
{
// what are the values of a, b, c, and d at this point (assuming
parameters are parsed right to left) ??
}
void main()
{
int z = 1973;
func(1973-z, --z, z=666, z=-1);
}