Contradiction about Order of Evaluation of operands

蹲街弑〆低调 提交于 2021-01-27 07:24:57

问题


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 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



来源:https://stackoverflow.com/questions/62051116/contradiction-about-order-of-evaluation-of-operands

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!