Why does printf() not print anything before sleep()?

前端 未结 4 594
死守一世寂寞
死守一世寂寞 2020-12-05 07:45

I\'m just learning C with Kernighan and Ritchie\'s book; I\'m in the basics of the fourth chapter (functions stuff). The other day I became curious about the sleep()

4条回答
  •  时光说笑
    2020-12-05 08:25

    Your problem is that printf (and anything else that uses the stdio library to write to stdout (standard output)) is buffered - line buffered if it goes to the console, and size buffered if it goes to a file. If you do a fflush(stdout); after the printf, it will do what you want. You could try just adding a newline ('\n') to your string, and that would do the right thing as long as you don't redirect standard output to a file.

    I'm not 100% sure, but I think stderr isn't buffered, which can cause confusion because you might see output you made to stderr before output you previously made to stdout.

提交回复
热议问题