【发布时间】:2015-12-01 19:41:10
【问题描述】:
我正在用 C 语言编写一个用于作业的程序,我想在我的 Windows 10 桌面上处理它,但是在使用 MinGW 时遇到了一个奇怪的问题。
我写的程序如下:
#include <stdio.h>
int main(){
//set up variables
int a, b, c, d;
//prompt user for integers
printf("Please enter four different integers:\n");
scanf("%d", &a);
scanf("%d", &b);
scanf("%d", &c);
scanf("%d", &d);
//return sum
int sum = a+b+c+d;
printf("Sum is: %d", sum);
return 0;
}
当我编译它时,输出如下所示:(其中 1、2、3 和 4 是输入的数字)
1
2
3
4
Please enter four different integers:
Sum is: 10
这显然不应该发生,因为这是不正常的。为了尝试排除故障,我在运行 Arch 的笔记本电脑上使用 GCC 编译了相同的代码,输出如下所示:(其中 1、2、3 和 4 是输入的数字)
Please enter four different integers:
1
2
3
4
Sum is: 10
这是输出应该的样子。我在 Linux 和 Windows 计算机上都使用 Eclipse Mars 作为 IDE。我还在另一台双启动 Windows 10 和 Ubuntu 的笔记本电脑上尝试了相同的操作,并且在 MinGW 和 GCC 之间获得了相同的结果。
如果有人知道为什么 MinGW 会这样做,我将不胜感激!谢谢!
【问题讨论】:
-
Flushing buffers in C 的可能重复项
-
您需要使用 fflush(stdout) 刷新缓冲区。见:stackoverflow.com/questions/12450066/flushing-buffers-in-c
-
我认为这与您的 IDE 使用的特定命令行有关,可能是输入/输出事件在处理时间上略有不同(即由 IDE 的命令行)