When I googled this I always got threads about order of evaluation in general stating order of evaluation is unspecified.
I know the parameter evaluation order is unspecified in C
in general.
My question is parameter evaluation order in gcc
, left to right or right to left ?
Any links to resources would also be appreciated...
EDIT: Removing ambiguity in the question
Well, I'm talking about the situation when
foo(1+2,2+3,4+9)
which is first evaluated?
is it 1+2
or 4+9
... like wise..
Can we come to a declaration by just compiling this in one gcc compiler....? or is it different across different gcc versions also?
If you are really asking foo(f1(), f2(), f3())
- which is more interesting than foo(1+2, 3+4, 5+6)
, since adding 1+2 and 3+4 won't have effect whether it is done first or last or in a random order.
Now, unfortunately, you can not rely on f1()
and f2()
and f3()
being called in any particular order - as long as each function is called ONCE, it's fine for the order to be any order:
f1, f2, f3
f1, f3, f2
f2, f3, f1
f2, f1, f3
f3, f2, f1
f3, f1, f2
(that covers all the permutations for three parameters).
It is entirely up to the compiler which it "thinks is best".
I wrote some code a long time back, and ran into this particular problem - I had something along the lines of:
char foo(char a, char b)
...
if (a =! 'x')
foo(fgetc(f), foo(fgetc(f)));
...
Since I expected the FIRST (left) fgetc()
to be called first, and the second fgetc()
, I could get the right behaviour. And it worked fine on the school computer. Then I took the code home and tried using it on my home computer. And for some reason it didn't work right. It took me quite some time to figure out that foo()
was just being called infinitely, because a
was never 'x'
, which stops the recursion - because 'x'
would never appear in the second call.
That was using gcc on both machines, but one was a sparc (school computer) and the one at home was a x86 (386, running OS/2, that's how long ago).
The solution is to break it into several lines:
char aa = fgetc(f);
char bb = fgetc(f);
foo(aa, foo(bb));