C standard I/O vs UNIX I/O basics C standard I/O vs UNIX I/O basics unix unix

C standard I/O vs UNIX I/O basics


write is a system call -- it is implemented by the interface between user mode (where programs like yours run) and the operating system kernel (which handles the actual writing to disk when bytes are written to a file).

printf is a C standard library function -- it is implemented by library code loaded into your user mode program.

The C standard library output functions buffer their output, by default until end-of-line is reached. When the buffer is full or terminated with a newline, it is written to the file via a call to write from the library implementation.

Therefore, the output via printf is not sent to the operating system write immediately. In your example, you buffer the letter 'u', then immediately write the letter 'm', then append "d\n" to the buffer and the standard library makes the call write(STDOUT_FILENO, "ud\n");


By default, stdout is line-buffered; it isn't flushed to the output until it encounters a newline character (or until the buffer fills up).

So the "u" sits in the buffer until the "d\n" is received. But the write bypasses this buffer.