本文介绍了printf 就在延迟之前在 C 中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有谁知道为什么如果我在延迟之前放一个 printf,它会等到延迟完成后才打印消息?
Does anyone know why if i put a printf just before a delay it waits until the delay is finished before it prints de message?
带有 sleep() 的代码 1:
Code1 with sleep():
int main (void)
{
printf ("hi world");
system("sleep 3");
}
具有自我实现延迟的代码 2:
Code2 with a self implemented delay:
void delay(float sec)
{
time_t start;
time_t current;
time(&start);
do{
time(¤t);
}while(difftime(current,start) < sec);
}
int main (void)
{
printf ("hi world");
delay(3);
}
如果:
printf ("hi world");
delay(3);
printf ("hi world");
delay(3);
它等待睡眠的总和,然后同时打印消息
it waits until the sum of sleeps and then it prints the messages at the same time
为什么会发生这种情况?
Why does this happen?
更新:我在调用延迟时写了延迟(睡眠3"),我的意思是延迟(3).更正
推荐答案
标准输出不会刷新,直到您输出 '\n' 字符.
the standard output is not flush until you output a '\n' char.
试试 printf ("hi world\n");
try printf ("hi world\n");
这篇关于printf 就在延迟之前在 C 中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!