C语言 printf 不打印到屏幕
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16870059/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
printf not printing to screen
提问by user1060986
If I try to run the following simple code under Cygwin on Windows 7,
如果我尝试在 Windows 7 上的 Cygwin 下运行以下简单代码,
#include <stdio.h>
int main() {
int i1, i2, sums;
printf( "Enter first integer\n" );
scanf( "%d", &i1 );
printf( "Enter second integer\n" );
scanf( "%d", &i2 );
sums = i1 + i2;
printf( "Sum is %d\n", sums );
return 0;
}
it compiles (via gcc) without a problem, but when I try to execute it, the first statement ("Enter first integer") isn't printed to the terminal, and I have to input two successive numbers (e.g. 3 and 4) before I get,
它编译(通过 gcc)没有问题,但是当我尝试执行它时,第一条语句(“输入第一个整数”)没有打印到终端,我必须输入两个连续的数字(例如 3 和 4)在我得到之前,
3
4
Enter first integer
Enter second integer
Sum is 7
Can anyone explain to me what is happening here. This works perfectly well under MinGW.
谁能向我解释这里发生了什么。这在 MinGW 下非常有效。
回答by zsawyer
Like @thejh said your stream seems to be buffered. Data is not yet written to the controlled sequence.
就像@thejh 说你的流似乎被缓冲了一样。数据尚未写入受控序列。
Instead of fiddling with the buffer setting you could call fflushafter each write to profit from the buffer and still enforce the desired behavior/display explicitly.
您可以fflush在每次写入后调用缓冲区设置,而不是摆弄缓冲区设置,以从缓冲区中获利,并且仍然明确地强制执行所需的行为/显示。
printf( "Enter first integer\n" );
fflush( stdout );
scanf( "%d", &i1 );
回答by Dayal rai
you can try for disabling the buffering in stdout by using
您可以尝试使用禁用标准输出中的缓冲
setbuf(stdout, NULL);
回答by thejh
It seems that the output of your program is buffered. Try enabling line buffering explicitly:
您的程序的输出似乎已被缓冲。尝试显式启用行缓冲:
setlinebuf(stdout);

