how to sleep in c [duplicate]

我只是一个虾纸丫 提交于 2019-12-04 22:58:22

问题


Possible Duplicate:
Why does printf not flush after the call unless a newline is in the format string?

When I run something like

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

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)?


回答1:


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);
}


来源:https://stackoverflow.com/questions/13385850/how-to-sleep-in-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!