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

标签 c recursion operator-precedence operands c-standard-library

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

相关文章:

c - printf 不会打印可见字符

c - 将数组传递给函数并通过递归添加它

javascript - 主干 Marionette 递归列表

R中的递归回归

php - AND、OR 输出错误结果

c - 数据结构 - C 中的链表库

c - 测试我的 h2c 实现的简单工具

linq - 递归地 (?) 将 LINQ 谓词组合成单个谓词

Java 复杂运算符优先级(后缀、一元、关系、逻辑 AND、赋值)

xslt - 如何在 XPATH 2.0 中使用 "<<"运算符?