1

By default, is STDOUT unbuffered? If not what is the type of its default buffering

Thanks

nitin_cherian
  • 6,405
  • 21
  • 76
  • 127

2 Answers2

5

You didn't give a language, but assuming that you're using C's stdio functions (fopen() etc.) or a language that uses these (and most do, for portability reasons):

It depends on the underlying C runtime library.

Most libraries will try to detect whether STDOUT is connected to a terminal, and avoid buffering if so, and perform block buffering (e.g. my Linux system buffers 8Kb at a time) if not.

j_random_hacker
  • 50,331
  • 10
  • 105
  • 169
  • 1
    Pretty sure this is wrong; `stdout` is typically line-buffered when connected to a terminal, not unbuffered. When all your outputs end in a newline, the difference is irrelevant, but if you're outputting without newlines (say, showing a "progress bar" by outputting a dot at a time), it will be buffered. Explicit calls to `fflush` would be needed to output partial lines on demand, or you'd use `setvbuf` to change the buffering mode for the `FILE*` in general. – ShadowRanger Feb 08 '18 at 18:35
  • @ShadowRanger: You're right, please edit to add this. – j_random_hacker Feb 09 '18 at 17:19
1

Short Answer: By default STDOUT is usually unbuffered. If this is a problem for you, there is fflush(stdout); but that is rarely needed

EnabrenTane
  • 7,428
  • 2
  • 26
  • 44
  • Again, as noted on accepted answer, `stdout` is typically line buffered when connected to a terminal, and block buffered otherwise. No common C runtime I'm aware of makes it unbuffered by default. – ShadowRanger Feb 08 '18 at 18:36