What are the rules of automatic stdout buffer flushing in C?

Andrii Zymohliad

I'm just curious which conditions should be satisfied to flush stdout buffer automatically.

First of all I was confused that this pseudo code doesn't print output every iteration:

while (1) {
    printf("Any text");
    sleep(1);
}

But if I add newline character it will.

After few experiments i found that on my machine stdout buffer is flushed:

  1. When I put to stdout 1025 characters or more;
  2. When I read stdin;
  3. When I put newline character to stdout;

The first condition is totally clear - when the buffer is full it should be flushed. The second one is also reasonable. But why newline character causes flushing? What are the others implicit conditions for this?

chux - Reinstate Monica

Rules of automatic flushing stdout buffer is implementation-defined (ID). It is ID when the stream is unbuffered, fully buffered, or line buffered.

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, ... C11dr §7.21.3 3


I'm just curious which conditions should be satisfied to flush stdout buffer automatically.

If code wants to insure output is certainly flushed, use fflush(). Other conditions that may automatically flush the stream are implementation defined.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

What does flushing the buffer mean?

In C, what's the size of stdout buffer?

What does "Write cache buffer flushing" mean

How can I view the stdout buffer before flushing it?

Python 3.6 on linux: Problem with flushing what i want to write to the stdout

What happens if STDOUT buffer is overflowed

Flushing stdout messages of c/c++ based executable

What are the rules for automatic upcasting of functions?

Flushing stdin buffer in Golang?

Automatic file buffer flushing when file opened by another program *only if* other program not already running (macOS)

What are the rules for automatic generation of move operations?

What are the deduction rules for automatic argument capture?

Client&Server flushing buffer

Flushing Twisted's write buffer

Asyncio server not flushing transport buffer

Flushing RxJava buffer on application close

What is the whole function and effect of "Turn off Windows write-cache buffer flushing on the device"

What is meant by 'flushing the stream'?

serilog async sink not flushing buffer to disk on shutdown

subprocess stdin buffer not flushing on newline with bufsize=1

What methods exist for capturing stdout into a buffer that is automatically cleared on read?

What are the rules for JavaScript's automatic semicolon insertion (ASI)?

C: What is a/example of a buffer underflow?

Non canonical terminal mode buffer stdout in c program

C# flushing StreamWriter and a MemoryStream

Capturing child stdout to a buffer

In C language, What is the difference between extern buffer[] and extern *buffer?

Why does stdout need explicit flushing when redirected to file?

flushing thread local buffer at end of parallel loop with TBB