问题
Is there some definite order in which the functions are called in the expression below or does it vary from compiler to compiler?
Does the following rule apply here - In C, the order in which arguments to functions and operands to most operators are evaluated is unspecified. Found the above rule in this wiki page
a = (f1(10, 20) * f2(30, 40)) + f3()
回答1:
The rule does apply. f1
, f2
, and f3
may be evaluated in any order.
To expand a bit on some of the confusion (because people keep posting wrong answers), operator precedence does not affect the order in which things are evaluated. Take this example:
f1() * f2() + f3()
Now, we know that yes, the results of f1()
and f2()
are multiplied, and then added to the result of f3()
, but we don't know the order of evaluation. The parse tree looks like this:
+
/ \
* f3()
/ \
f1() f2()
But we don't know if the left side or the right side of +
will be evaluated first. It could be either way. Same with *
: we don't know if its left side or right side will be evaluated first.
The compiler could call f3()
, then store the result, then call f2()
, store that result, and then call f1()
, and then use the stored results to perform the actual computation.
Or, alternatively, it could call f1()
, store the result, then call f2()
, then use the two values to multiply (and then store that result), then call f3()
and finish the computation.
In each of these cases (or any other permutation of evaluation order), the functions were evaluated in different order, and yet the same answer is achieved.
To conclude: operator precedence determines the parse tree, not the order of evaluation. The order in which the parse tree is evaluated is unspecified.
回答2:
A compiler may call those functions in any order (that is, the only guarantee is that f1 and f2 are called before the multiplication, and all three are called before the addition, and that all three will only be called after the value of their arguments is known).
With literal arguments like that, the compiler may well call them as early as possible (like, lines and lines above), or even make them inline.
Most importantly, don't confuse operator precedence with the order of function evaluation.
All you know is that the function will be called after the value of its arguments are known, but before its return value is used.
回答3:
These functions can be evaluated in any order. There is no specified rule. Either f1
is evaluated first then f2
and then f3
or f3
is evaluated first then f2
and then f1
or f2
is evaluated first then f1
and then f3
or even f1
is evaluated first then f3
and then f2
(total of six combinations here!).
One thing note here that: Operator precedence has nothing to do with the order of evaluation.
But keep in mind that what ever be the order of evaluation of these functions the arithmetic operation on the expression
a = (f1(10, 20) * f2(30, 40)) + f3()
will take palace according to the operator precedence rule, i.e, f1*f2
takes place first (at the time f1
and f2
must be evaluated) and then the result of this is added to f3
(all f1
, f2
and f2
must be evaluated).
来源:https://stackoverflow.com/questions/20756139/which-order-functions-would-be-called