关于操作数求值顺序的矛盾 [英] Contradiction about Order of Evaluation of operands

查看:78
本文介绍了关于操作数求值顺序的矛盾的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我从 deitel c 学习 C 中的递归函数时,我读到了这句话:

When I study recursive functions in C from deitel c, I read this sentence:

标准 C 没有指定大多数运算符(包括 +)的操作数的计算顺序.

但书中也说:

从左到右'+'的结合性

操作数的求值顺序:

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

Could anyone explain why this is?

推荐答案

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

Order of evaluation and associativity are two different things, take the example:

int x = func1() - func2() - func3(); //having int return types

在这个表达式中,你不知道 func1() 将首先被评估还是最后被评估,这意味着,你不知道哪个函数将被调用并首先返回它的值,但是你知道与 + 一样,结合性将从左到右,首先是 func1() - func2() 然后是减法的结果 - func3().

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().

C 中没有从左到右或从右到左求值的概念,不要与运算符的从左到右和从右到左结合性混淆:表达式f1() + f2() + f3() 被解析为 (f1() + f2()) + f3() 由于operator+,但对 f3() 的函数调用可能首先、最后或在 f1()f2() 之间进行评估 在运行时.

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

这篇关于关于操作数求值顺序的矛盾的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆