关于在linux中运行sleep()的命令

来源:百度知道 编辑:UC知道 时间:2024/09/18 03:30:34
#include<stdio.h>
int main()
{
printf("hello");
sleep(3);
printf("after 3s");
return 0;
}
在TC2.0中会先显示hello,过3秒才显示after 3s,但在linux中用C语言却发现是等3秒后才把hello after 3s 全都显示出来。为什么呢?

行缓冲!你要修改成这样。
#include<stdio.h>
int main()
{
printf("hello\n");
sleep(3);
printf("after 3s\n");
return 0;
}
或者printf后面添加fflush,
printf("hello");
fflush(stdout);

没有吧,我试了.

你在第一个printf后加一句。

fflush(stdout);