终端上不打印单个字符
我有 3 个不同的进程,它们都使用 printf
打印出单个字符。但我在航站楼看不到它们。当我添加换行符 printf("\n H")
时,每个字符都在新行上,我可以看到它们。为什么没有换行符就不起作用?
I have 3 different processes that all print out single characters using printf
. But I can't see them in the terminal. When I add a newline, printf("\n H")
so each character is on a new line, I can see them. Why doesn't it work without the newline character?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是冲水的问题。如果您在每次 printf 之后刷新缓冲区,您应该得到更接近您想要的输出。要刷新标准输出,只需执行 fflush( stdout ) 即可。
Its a matter of flushing. If you flush the buffers after each
printf
, you should get output closer to what you want. To flush the standard output simply dofflush( stdout )
.C 标准为输出流定义了 3 种类型的缓冲:
输出流的缓冲类型可以通过 setvbuf( 3) 和
setbuf(3)
函数。C 标准要求 stderr 在启动时不完全缓冲(在许多实现中通常是不缓冲的,以便尽快发现错误);仅当可以确定 stdout 不引用终端时, stdout 才会完全缓冲(当它引用终端时,许多实现将其初始化为行缓冲,这就是您所看到的) 。
The C standard defines 3 types of buffering for output streams:
An output stream's buffering type can be changed via the
setvbuf(3)
andsetbuf(3)
functions.The C standard requires
stderr
to not be fully-buffered at startup (it is usually unbuffered on many implementations, so as to see errors as soon as posible); andstdout
to be fully-buffered only if it can be determined to not refer to a terminal (when it refers to a terminal, many implementations initialize it as line-buffered, which is what you are seeing).使用'write(1,&c,1)'系统调用,或者
use'write(1,&c,1)' system call, or