gpt4 book ai didi

关于操作数求值顺序的矛盾

转载 作者:行者123 更新时间:2023-12-03 21:13:37 25 4
gpt4 key购买 nike

当我从 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



操作数的计算顺序:

order of evaluation of operands

谁能解释一下这是为什么?

最佳答案

求值顺序和关联性是两个不同的东西,举个例子:

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 of operator+, but the function call to f3() may be evaluated first, last, or between f1() or f2() at run time.



https://en.cppreference.com/w/c/language/eval_order

关于关于操作数求值顺序的矛盾,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62051116/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com