printf什么时候打印失败?

发布于 2025-01-05 22:59:13 字数 129 浏览 0 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

萝莉病 2025-01-12 22:59:13

标准输出是一个缓冲流,除非放入换行符、关闭流或程序正常退出,否则不保证刷新。如果程序异常退出,有可能流没有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.

陈独秀 2025-01-12 22:59:13

这并不是说 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 call
fflush(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.

债姬 2025-01-12 22:59:13

我之所以使用它

puts(largeString); 

,是因为在我的特定情况下,printf() 刚刚停止打印。整个字符串都在那里,只是没有打印出来。

fflush(stdout) 也没有修复它,下一行的另一个 printf() 打印得很好。

I used

puts(largeString); 

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文