Search code examples
cbufferstdin

In C what buffering state is stdin by default?


This may be a completely wrong question or at the very least pedantic but I'm working through K&R and I'm reading through this documentation: File input/output

And though it states that streams have a buffering state it does not mention what state standard input is in by default.

Is it line buffered by default? Is it implementation specific?

Running this code seems to indicate it is line buffered:

#include <stdio.h>

main()
{
    getchar();
}

Thanks


Solution

  • It's a good question, but the answer is rather non-specific.

    The C standard specifically states that stdin and stdout are, in their initial state, fully buffered if and only if (my emphasis) the stream does not refer to an interactive device. That would suggest that anything else is unspecified and/or implementation-defined behavior. The standard also specifies that the definition of interactive device is implementation-defined.

    Update: to quote the relevant portions of the most recent standard:

    C17 § 7.21.3p7:

    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

    C17 § 5.1.2.3p7:

    What constitutes an interactive device is implementation-defined.

    The language in the C99 and C11 standards is identical, though the section numbering is different.