Printf和睡在For Loop里面? [英] Printf and sleep inside for loop?
本文介绍了Printf和睡在For Loop里面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我不明白为什么下面的代码是这样工作的.我的意思是:不是在每一秒延迟后打印"Hello"...它等待5秒并立即显示hellohellohellohellohello
。
#include <stdio.h>
int i;
for(i=0; i<5; i++) {
printf("hello");
sleep(1);
}
推荐答案
如果输出要发送到TTY,printf()
(stdout
)的输出默认情况下是行缓冲的。您需要
printf("hello
");
或
printf("hello");
fflush(stdout);
后者将在每次迭代中显式刷新输出。
这篇关于Printf和睡在For Loop里面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文