Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
for(j = 1; j < 11; j++) { printf("%ds ",j); sleep(1); }
我预计这段代码将打印“1s 2s 3s ... 10s”,它们之间有 1 秒的间隔,但它会在 10 秒后打印所有数字。我该如何解决?我正在开发 ubuntu。
流是缓冲的stdout,因此如果您立即想要结果,则需要使用以下命令刷新该缓冲区fflush():
stdout
fflush()
for (j = 1; j < 11; j++) { printf("%ds ", j); fflush(stdout); sleep(1); }
将 \n 添加到您的 printf 以进行刷新。
for(j = 1; j < 11; j++) { printf("%ds\n",j); sleep(1); }
有关更多信息,请参见此处。