printf的不打印到屏幕 [英] printf not printing to screen

查看:1189
本文介绍了printf的不打印到屏幕的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我尝试在Windows 7上运行在Cygwin下下面的简单code,

If I try to run the following simple code under Cygwin on Windows 7,

#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;
}

它编译(通过GCC)没有问题,但是当我尝试执行它,第一条语句(输入第一个整​​数)不打印到终端,而我必须输入两个连续的号码(如:3 4)之前,我,

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,

3
4
Enter first integer
Enter second integer
Sum is 7

任何人都可以向我解释这里发生了什么。这工作得很好的MinGW下。

Can anyone explain to me what is happening here. This works perfectly well under MinGW.

推荐答案

就像@thejh说你的信息流中进行缓冲。数据还没有写入控制序列。

Like @thejh said your stream seems to be buffered. Data is not yet written to the controlled sequence.

而不是与缓冲区设置摆弄你可以调用 fflush 之后,每次写从缓冲区中获利,仍然执行所需的行为/显示明确的。

Instead of fiddling with the buffer setting you could call fflush after each write to profit from the buffer and still enforce the desired behavior/display explicitly.

printf( "Enter first integer\n" );
fflush( stdout );
scanf( "%d", &i1 );

这篇关于printf的不打印到屏幕的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆