<分区>
我一直在寻找 C 的某些领域,在那里我们可以通过不同的编译器获得不同的行为。
查看以下代码,我们在 gcc4.4.5 中得到输出 7,在 tcc4.5 中得到输出 10。
int i=0;
i=i++ + ++i + i++ + ++i;
printf("%d",i);
我们能否确定我们获得不同行为的其他使用领域,为了良好的编程习惯应该避免这些行为。
[注:以上指定代码仅为示例。问题不仅限于此。]
标签 c
<分区>
我一直在寻找 C 的某些领域,在那里我们可以通过不同的编译器获得不同的行为。
查看以下代码,我们在 gcc4.4.5 中得到输出 7,在 tcc4.5 中得到输出 10。
int i=0;
i=i++ + ++i + i++ + ++i;
printf("%d",i);
我们能否确定我们获得不同行为的其他使用领域,为了良好的编程习惯应该避免这些行为。
[注:以上指定代码仅为示例。问题不仅限于此。]
最佳答案
结果是“编译器相关的”,因为就语言而言,它是未定义的行为。
Except where noted, the order of evaluation of operands of individual operators and subexpressions of individual expressions, and the order in which side effects take place, is unspecified. Between the previous and next sequence point a scalar object shall have its stored value modified at most once by the evaluation of an expression. Furthermore, the prior value shall be accessed only to determine the value to be stored. The requirements of this paragraph shall be met for each allowable ordering of the subexpressions of a full expression; otherwise the behavior is undefined.
关于c - C 编译器的行为差异示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4966172/