0

I have noticed printf only printing to the screen...

  • when there is newline
  • when there is scanf

Help me understand this behavior. I am thinking that printf writes to stdout and the kernel is flushing to screen or display drivers. Is my understanding correct?

Prathibha
  • 69
  • 2
  • 9
  • 2
    Unless you use `fflush()`, what you describe is basically correct. One other case where the data is printed is when the buffer fills. You can find the default buffer size from `BUFSIZ` in ``. It is the standard I/O code that decides when to call on the kernel to write the data in the buffer, but it is then the kernel (in conjunction with the window management system) that ensures the data is written to the right places. – Jonathan Leffler Aug 08 '18 at 18:51
  • 1
    Minor quibble: any input function (e.g. getchar, scanf, fgets) will flush the buffer. – user3386109 Aug 08 '18 at 19:00

2 Answers2

3
7.21.3 Files
...
3 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, and may be affected via the setbuf and setvbuf functions.
...
7 At program startup, three text streams are predefined and need not be opened explicitly — standard input (for reading conventional input), standard output (for writing conventional output), and standard error (for writing diagnostic output). As initially opened, the standard error stream is not fully buffered; 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.

C 2011 online draft

On most interactive environments, standard input and standard output are line-buffered. So yeah, output will be buffered until a newline is seen, or the output operation is immediately followed by an input operation (or fflush call).

Community
  • 1
  • 1
John Bode
  • 119,563
  • 19
  • 122
  • 198
0

You're correct. The function printf() sends formatted output to stdout.

int printf(const char *format, ...);

To quote from another post:

printf() statements sends output to an intermediate storage called buffer. Every now and then, the material in the buffer is sent to the screen. The standard C rules for when output is sent from the buffer to the screen are clear:

  1. It is sent when the buffer gets full.
  2. When a newline character is encountered.
  3. When there is impending input.
Alex Johnson
  • 958
  • 8
  • 23
  • 3
    How does this answer the question? It was about *when* output is sent to the device. – Weather Vane Aug 08 '18 at 18:54
  • @WeatherVane Quoted from the wrong post. Fixed. – Alex Johnson Aug 08 '18 at 18:59
  • Please quote from the C standard, not another post. C does not mention "screen" or say what an OS is supposed to do. – Weather Vane Aug 08 '18 at 19:03
  • 1
    Note [C11 §7.21.3 Files ¶7](http://port70.net/~nsz/c/c11/n1570.html#7.21.3p7) says: _At program startup, three text streams are predefined and need not be opened explicitly -- standard input (for reading conventional input), standard output (for writing conventional output), and standard error (for writing diagnostic output). As initially opened, the standard error stream is not fully buffered; 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._ – Jonathan Leffler Aug 08 '18 at 19:04
  • Although many systems do synchronize between standard output and standard input when there's an input operation, the standard doesn't actually make that a requirement. – Jonathan Leffler Aug 08 '18 at 19:09