问题
When I study recursive functions in C from deitel c, I read this sentence:
Standard C does not specify the order in which the operands of most operators (including +) are to be evaluated.
But also the book says that:
associativity of '+' from left to right
Order of evaluation of operands:
Could anyone explain why this is?
回答1:
Order of evaluation and associativity are two different things, take the example:
int x = func1() - func2() - func3(); //having int return types
In this expression you can't know if func1()
will be evaluated first or last, meaning, you don't know which function will be called and return its value first, however you know that the associativity, as with +
, will be left-to-right, first func1() - func2()
then the result of that subtraction - func3()
.
There is no concept of left-to-right or right-to-left evaluation in C, which is not to be confused with left-to-right and right-to-left associativity of operators: the expression
f1() + f2() + f3()
is parsed as(f1() + f2()) + f3()
due to left-to-right associativity ofoperator+
, but the function call tof3()
may be evaluated first, last, or betweenf1()
orf2()
at run time.
https://en.cppreference.com/w/c/language/eval_order
来源:https://stackoverflow.com/questions/62051116/contradiction-about-order-of-evaluation-of-operands