c - GCC 和 Borland Turbo C 中 printf() 语句的不同输出

标签 c gcc turbo-c

<分区>

我有一个C语言的小代码

#include<stdio.h>
int main()
{
    int a=10,b;
    b=a++ + ++a;
    printf("%d,%d,%d,%d",b,a++,a,++a);
    return 0;
}

Turbo C 给出以下输出(如预期的那样)

22,13,13,13

但是 GCC(在 windows 中使用 ubuntu 和代码块编译器)给出了以下内容

22,13,14,14

我相信 Turbo c 输出是正确的,但 GCC 怎么会返回不同的输出?

最佳答案

他们都是对的!这是未定义的行为,因为您不允许在单次调用中多次更改相同的值。

来自 c99 ISO/IEC 9899:TC3 -> 附录 J:

J.2 Undefined behavior 1 The behavior is undefined in the following circumstances:

[...]

— Between two sequence points, an object is modified more than once, or is modified and the prior value is read other than to determine the value to be stored (6.5).

编辑:

关于告诉 turbo c 早于 c99 的 icepacks 评论,我还添加了来自

C89 标准程序设计语言 C, X3.???-1988:

A.6.2 Undefined behavior The behavior in the following circumstances is undefined:

[..]

  • An object is modified more than once, or is modified and accessed other than to determine the new value, between two sequence points (3.3).

关于c - GCC 和 Borland Turbo C 中 printf() 语句的不同输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18802803/

相关文章:

c - 变量的前置和后置增量操作在 TC 和 gcc 上给出不同的输出

检查文件夹是否可写

c - 在 Makefile.am(automake) 中使用 if 语句

c++ - 在 amd64 Debian 上编译 i386 二进制文件?

c - Linux 上的 <conio.h> 头文件在哪里?为什么我找不到 <conio.h>?

c - 我如何执行 "Millions of Calculations?"

c - 如何使用c中的像素值在GUI窗口中绘制图片?

c - 在不使用数组的情况下交换给定数字的备用数字

c - 为什么在文件范围内初始化枚举类型变量时出现错误?

c - 从 c 中的管道读取并打印到标准输出