c - C 编译器的行为差异示例

标签 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/

相关文章:

c - 给定一些文本输入,找到最大的字谜组

c++ - 如何在 Makefile 的 pathsubst 中添加 "or"

c - 我怎样才能让数组从用户那里读取?

c - 这个校验和计算有什么问题吗?

c - 在 Linux 中从 C 杀死所有进程

c++ - 为什么树向量化会使这种排序算法慢2倍?

java - JNA。 char* 不包含结果

我可以在 Ubuntu 9.10 上使用 gcc 交叉编译旧版本的 Linux 发行版吗?

c - 到达迷宫中任意一点的最短路径

c - 段错误: 11