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