且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

C和JAVA中增量递减运算符的差异

更新时间:2023-11-14 15:36:52

对于 c99 c> 标准草案 6.5.2


在上一个和下一个序列点之间,一个对象应该通过对表达式的求值最多修改一个对象的
。此外,先前的值
应该是只读的,以确定要存储的值。

Between the previous and next sequence point an object shall have its stored value modified at most once by the evaluation of an expression. Furthermore, the prior value shall be read only to determine the value to be stored.

它引用了以下代码示例作为未定义:

it cites the following code examples as being undefined:

i = ++i + 1;
a[i++] = i; 

该部分在 2011年草案标准,但它读起来有点尴尬。这是序列点的好参考。

The section is same in the draft 2011 standard as well but it reads a bit more awkward. This is a good reference on sequence point.

部分 15.7 JLS 中的相关部分:


Java编程语言保证操作符的操作数似乎以特定的评估顺序进行评估,即从左到右。

The Java programming language guarantees that the operands of operators appear to be evaluated in a specific evaluation order, namely, from left to right.

建议代码不要严格依赖于此规范。每个表达式最多只包含一个副作用时,代码通常更清晰。

It is recommended that code not rely crucially on this specification. Code is usually clearer when each expression contains at most one side effect