gpt4 book ai didi

c - 表达式的右手边总是先求值

转载 作者:行者123 更新时间:2023-12-01 08:28:49 24 4
gpt4 key购买 nike

右侧是否总是评估在左侧之前?然后右侧的结果将传递到左侧。我不是在谈论诸如 A[i]=i++ 之类的异常

我说的是正常情况:

A[i] = (j+32+43 & K); 
A[j] != (A[j] + A[k]);

会先评估所有这些表达式的右侧部分,然后将结果与左侧进行比较吗? (总是)

最佳答案

一般来说,子表达式的计算顺序是不确定的,有一些异常(exception),例如逻辑与、逻辑或、逗号运算符等......

由于您 comment 表示您对一般规则感兴趣:

any operstor @YuHao if there is any general rule



这将由 draft C99 standard 部分 6.5 Expressions 段落 3 涵盖(强调我的 future ):

The grouping of operators and operands is indicated by the syntax.74) Except as specified later (for the function-call (), &&, ||, ?:, and comma operators), the order of evaluation of subexpressions and the order in which side effects take place are both unspecified.



这在 draft C11 standard 中基本相同,但 C11 未列出异常,因此引用 C99 更方便。 C11 中的段落 3 说:

The grouping of operators and operands is indicated by the syntax.85) Except as specified later, side effects and value computations of subexpressions are unsequenced.86)



专门针对赋值运算符 C99 说:

The order of evaluation of the operands is unspecified [...]



C11 说:

[...] The evaluations of the operands are unsequenced.

关于c - 表达式的右手边总是先求值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26351162/

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