Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does printf always flush the buffer on encountering a newline?

Tags:

c

posix

My machine is running ubuntu 10.10, and I'm using the standard gnu C library. I was under the impression that printf flushed the buffer if there was a newline described in the format string, however the following code repeatedly seemed to buck that trend. Could someone clarify why the buffer is not being flushed.

#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/wait.h>

int main()
{
    int rc;
    close(1);
    close(2);
    printf("HI 1\n");
    fprintf(stderr, "ERROR\n");

    open("newfile.txt", O_WRONLY | O_CREAT | O_TRUNC, 0600);
    printf("WHAT?\n");
    fprintf(stderr, "I SAID ERROR\n");

    rc = fork();

    if (rc == 0)
    {
        printf("SAY AGAIN?\n");
        fprintf(stderr, "ERROR ERROR\n");
    }
    else
    {
        wait(NULL);
    }

    printf("BYE\n");
    fprintf(stderr, "HI 2\n");

    return 0;
}

The contents of newfile.txt after running this program is as follows.

HI 1
WHAT?
SAY AGAIN?
BYE
HI 1
WHAT?
BYE
like image 667
Varun Madiath Avatar asked Mar 08 '11 06:03

Varun Madiath


People also ask

Does printf flush on newline?

To clarify the title of the question: printf(..) does not do any flushing itself, it's the buffering of stdout that may flush when seeing a newline (if it's line-buffered). It would react the same way to putchar('\n'); , so printf(..) is not special in this regard.

Does newline flush buffer?

Yes, when the file stream is closed at the (normal) end of the program, pending output will be flushed. It'll also be flushed when the buffer is full.

Does printf flush the buffer?

The buffer is flushed when it is full.

Does printf print a newline?

The printf statement does not automatically append a newline to its output. It outputs only what the format string specifies. So if a newline is needed, you must include one in the format string.


2 Answers

No, the standard says that stdout is initially fully buffered if the output device can be determined to be a non-interactive one.

It means that, if you redirect stdout to a file, it won't flush on newline. If you want to try and force it to line-buffered, use setbuf or setvbuf.

The relevant part of C99, 7.19.3 Files, paragraph 7, states:

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.

Just keep in mind section 5.1.2.3/6:

What constitutes an interactive device is implementation-defined.

like image 135
paxdiablo Avatar answered Sep 25 '22 15:09

paxdiablo


It is flushed if the output device is an interactive one e.g., a terminal.

You have to flush the output buffer in case the output device can be determined to be non-interactive e.g., a file. New line does not do that automatically.

For details see paxdiablo's answer.

like image 37
Ozair Kafray Avatar answered Sep 22 '22 15:09

Ozair Kafray