printf什么时候打印失败?
C 中的 printf 函数并不总是在屏幕上打印输出。例如,如果您忘记将 \n 放在字符串末尾,则您正在 printfing,有时您不会得到 o/p。 printf 不打印时是否还有其他一些情况?我记得有人说过这样的条件有7个。你们可以帮忙吗?
printf function in c doesn't always print the output on screen. For example if you forget to put \n at the end of string you are printfing you sometimes don't get the o/p. Are there some other conditions when printf doesn't print. I remember someone saying that there are 7 such conditions. Can you guys please help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
标准输出是一个缓冲流,除非放入换行符、关闭流或程序正常退出,否则不保证刷新。如果程序异常退出,有可能流没有flush。标准输出是行缓冲的,这就是换行符会刷新它的原因。有些缓冲区不会用换行符刷新。
Standard out is a buffered stream, it is not guaranteed to flush unless a newline is put in, the stream is closed, or the program exits normally. If the program exits abnormally, it is possible for the stream to not flush. Standard out is line buffered, which is why a newline will flush it. There are buffers that will not flush with a newline.
这并不是说
printf
不会总是打印,而是不能保证立即打印。这意味着,如果您将它用于调试目的,那么您无法保证它会与代码中的情况完全一致。如果您想确保它确实按照您所说的时间打印fflush(stdout)
。注意:你通常不想使用
fflush(stdout)
除非你正在调试,它确实是资源密集型的,如果你关心速度性能,它有潜力让你慢下来。its not that
printf
won't always print, its that it isn't guaranteed to print immediately. This means that if you are using it for debugging purposes, then you can't guarantee that it will happen exactly when it does in the code. If you want to make sure that it does print exactly when you said it callfflush(stdout)
.Note: You typically don't want to use
fflush(stdout)
unless you are debugging, its really resource intensive and if you care about speed performance at all it has the potential to slow you down.我之所以使用它
,是因为在我的特定情况下,printf() 刚刚停止打印。整个字符串都在那里,只是没有打印出来。
fflush(stdout) 也没有修复它,下一行的另一个 printf() 打印得很好。
I used
because in my particular case, printf() just stopped printing halfway through. The entire string was there, it just didn't print.
fflush(stdout) didn't fix it either, another printf() on the next line printed just fine.