简体   繁体   English

'print'在printf函数返回时意味着什么?

[英]What does 'transmitted' mean in printf function return?

I'm confused the interpretation of printf 's return value and buffered stream in Standard C Library. 我对标准C库中printf的返回值和缓冲流的解释感到困惑。

In C99:TC3 Standard , 7.19.6.3/p3 defines that printf function returns non-negative "number of characters transmitted " in success. C99:TC3标准中 ,7.19.6.3 / p3定义printf函数成功返回非负“ 传输的字符数”。 Also, 7.19.3/p3 describes the behaviors of fully/line buffered stream with " transmitted to or from the host environment", and p7 says stdout can be fully buffered stream. 此外,7.19.3 / p3描述了“ 传输到主机环境或从主机环境传输 ”的完全/线路缓冲流的行为,而p7表示stdout可以是完全缓冲的流。

Quote section 7.19.3 with emphasis added: 引用第7.19.3节,重点是:

7.19.3 Files 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. 3当流未缓冲时 ,字符应尽快从源或目标出现。 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. 对这些特性的支持是实现定义的,可能会受到setbufsetvbuf函数的影响。

7 [...] As initially opened, the standard error stream is not fully buffered; 7 [...]最初打开时,标准错误流未完全缓冲; the standard input and standard output streams are fully buffered if and only if the stream can be determined not to refer to an interactive device. 当且仅当可以确定流不参考交互设备时,标准输入和标准输出流被完全缓冲

These definitions lead the following behavior is legal. 这些定义导致以下行为是合法的。 But it's counterintuitive and unacceptable result (at least for me). 但这是违反直觉和不可接受的结果(至少对我而言)。

#include <stdio.h>
#include <assert.h>

// PRECONDITION: `stdout` is fully buffered
int main()
{
   int n = printf("abc");  // "abc" is accumulated, and no transmission.
   assert(n == 0);         // so, return value can be equal to 0 ??
}

What am I wrong in my interpretation? 我的解释错在哪里? Or it's only one of "implementation-defined" behavior? 或者它只是“实现定义”行为之一?

The return value of printf and fprintf are the number of bytes transmitted to the stream -- which is an object inside of the host environment -- not to the final destination. printffprintf的返回值是传输到流的字节数 - 它是主机环境中的对象 - 而不是最终目标。 When and how those bytes are transmitted by the stream to the file or device ("transmitted from the host environment") is irrelevant. 这些字节何时以及如何通过流传输到文件或设备(“从主机环境传输”)是无关紧要的。 Buffering does not affect how the stream accepts bytes from the program; 缓冲不会影响流如何从程序接受字节; just whether and until when it holds onto them until sending them on to the associated file or device. 是否直到将它们发送到相关文件或设备上时才会保留它们。

The word " transmitted " implies that the bytes cross some interface. 传输 ”一词意味着字节穿过某个接口。 I submit that the interface that 7.19.6.3 refers to is the interface between printf and the device driver, whereas the interface that 7.19.3 refers to is the output of the device driver. 我认为7.19.6.3引用的接口是printf和设备驱动程序之间的接口,而7.19.3引用的接口是设备驱动程序的输出。

Furthermore, I submit that the interpretation you propose would make the return value from printf arbitrary and capricious. 此外,我认为你提出的解释会使printf的返回值变得任意和反复无常。

Hence, I conclude that the printf in your sample code will always return 3. 因此,我得出结论,示例代码中的printf将始终返回3。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM