printf除非换行符在格式字符串中,为什么在调用后不刷新?这是POSIX的行为吗?我怎么可能printf每次都立即冲洗?
我只是用Kernighan和Ritchie的书学习C语言; 我在第四章的基础知识(函数).前几天我对这个sleep()功能感到好奇,所以试着像这样使用它:
#include <stdio.h>
#include <unistd.h>
int main(void)
{
printf(" I like cows.");
sleep(5);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
问题是程序的输出,它看起来像sleep()第一个然后printf(),换句话说,它等待五秒然后打印字符串.所以我想,也许程序变得sleep()如此之快,以至于它不会让printf()我的工作按照我的意愿完成,即打印字符串然后再睡觉.
如何显示字符串然后让程序进入睡眠状态?编译器是OpenBSD 4.3中的GCC 3.3.5(propolice).
PS我不知道你如何正确地放置预处理器线.
我无法理解为什么下面的代码会像这样工作..我的意思是:在每一秒延迟后不再打印"你好"......它会等待5秒并立即显示hellohellohellohellohello.
#include <stdio.h>
int i;
for(i=0; i<5; i++) {
printf("hello");
sleep(1);
}
Run Code Online (Sandbox Code Playgroud)