今天我了解到 stdout 在设置为终端时是行缓冲的,并且在不同情况下是缓冲的。因此,在正常情况下,如果我使用 printf() 而不终止 '\n',只有当缓冲区已满时,它才会打印在屏幕上。如何获得这个缓冲区的大小,它有多大?
实际大小由各个实现定义;该标准没有规定最小尺寸(无论如何,基于我所能找到的)。不知道如何确定缓冲区的大小。
Edit
章节和诗句 http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf:
7.19.3 文件
...
3 When a stream is unbuffered, characters are intended to appear from the source or at the
destination as soon as possible. Otherwise characters may be accumulated and
transmitted to or from the host environment as a block. When a stream is fully buffered,
characters are intended to be transmitted to or from the host environment as a block when
a buffer is filled. When a stream is line buffered, characters are intended to be
transmitted to or from the host environment as a block when a new-line character is
encountered. Furthermore, characters are intended to be transmitted as a block to the host
environment when a buffer is filled, when input is requested on an unbuffered stream, or
when input is requested on a line buffered stream that requires the transmission of
characters from the host environment. Support for these characteristics is
implementation-defined, and may be affected via the setbuf
and setvbuf
functions.
添加了强调。
“实现定义”并不是“我不知道”的委婉说法,它只是一种声明,表明语言标准明确地将其留给了执行 to define行为。
话虽如此,有is以非程序化的方式找出答案;请参阅您的编译器的文档。 “实现定义”也意味着实现must记录行为:
3.4.1
1 implementation-defined behavior
unspecified behavior where each implementation documents how the choice is made
2 EXAMPLE An example of implementation-defined behavior is the propagation of the high-order bit
when a signed integer is shifted right.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)