如何在C睡 [英] how to sleep in c

查看:158
本文介绍了如何在C睡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:结果
  <一href=\"http://stackoverflow.com/questions/1716296/why-does-printf-not-flush-after-the-call-unless-a-newline-is-in-the-format-strin\">Why除非换行符是格式字符串的printf确实没有呼叫后冲洗?

当我运行像

for (i = 1; i <= 10; i++) {
    sleep(1);
    printf(".");
}

那么我期望是每秒10次一个点。我得到10秒后就是十点一次。为什么会这样,而且我怎么让程序实际打印一个点(或做其他事情)每秒(或不同的时间间隔)?

then what I would expect is one dot per second ten times. What I get is ten dots once after ten seconds. Why is that so, and how do I get the program to actually print one point (or do other things) each second (or different time interval)?

推荐答案

的printf()正在缓冲数据,你可以用<迫使它刷新数据code> fflush(标准输出):

The printf() is buffering the data, you can force it to flush that data with fflush(stdout):

for (i = 1; i<=10; i++) 
{  
    sleep(1); 
    printf("."); 
    fflush(stdout);
}

这篇关于如何在C睡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆