当我从 deitel c 学习 C 中的递归函数时,我读到了这句话:
Standard C does not specify the order in which the operands of most operators (including +) are to be evaluated.
但书中也说:
associativity of '+' from left to right
操作数的计算顺序:
谁能解释一下这是为什么?
最佳答案
求值顺序和关联性是两个不同的东西,举个例子:
int x = func1() - func2() - func3(); //having int return types
在这个表达式中你不知道是否
func1()
将首先或最后被评估,这意味着,您不知道将调用哪个函数并首先返回其值,但是您知道关联性,如 +
, 将从左到右,第一个 func1() - func2()
那么减法的结果- 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
关于关于操作数求值顺序的矛盾,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62051116/