我正在用C編寫一個程序,我想在Windows 10桌面上工作,但是在使用MinGW時遇到了一個奇怪的問題。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
這顯然不應該發生,因爲這是無序的。要嘗試解決問題,我編譯使用GCC上我的筆記本電腦上運行拱相同的代碼和輸出是這樣的:(其中1,2,3和4是輸入的號碼)
Please enter four different integers:
1
2
3
4
Sum is: 10
這是輸出應該是的樣子。我在Eclipse和Windows計算機上使用Eclipse Mars作爲IDE。我也在我的其他筆記本電腦上嘗試過,它可以啓動Windows 10和Ubuntu,並且在MinGW和GCC之間也有相同的結果。
如果有人有任何想法,爲什麼MinGW會採取這種行動,我將不勝感激!謝謝!
[C中的Flushing緩衝區]的可能重複(http://stackoverflow.com/questions/12450066/flushing-buffers-in-c) –
您需要使用fflush(stdout)刷新緩衝區。請參閱:http://stackoverflow.com/questions/12450066/flushing-buffers-in-c –
我相信這是關於您的IDE使用的特定命令行,可能是導致輸入/輸出事件的時間稍有不同(即通過IDE的命令行) –