This question already has answers here:
Closed 2 years ago.
“printf” doesn't print a string immediatly [duplicate]
(2个答案)
下面是一些简单的代码;请注意,在调用
(2个答案)
下面是一些简单的代码;请注意,在调用
printf
之前有一个waitFor()
语句。为什么程序会暂停3秒钟然后打印消息?int main(int argc, char* argv)
{
producer();
return 0;
}
void waitFor(unsigned int secs) {
unsigned int retTime = time(0) + secs; // Get finishing time.
while (time(0) < retTime); // Loop until it arrives.
}
static void *producer()
{
int s = 3;
printf("Busy for %d seconds", s);
waitFor(s);
return NULL;
}
最佳答案
你是怎么运行这个程序的?有时Prtff()的输出被缓冲,只在打印大量数据或程序退出时才出现。有两种简单的修复方法:
使用stderr:printf(
>fprintf(stderr,
使用后刷新:printf后调用fflush(stdout)
关于c - Printf没有按程序顺序执行,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40712128/