Un-Buffering Output

Last year, I wrote about using the standard error device stderr to generate output that can’t be redirected. This technique is often used to send error messages to the console, though I noted how these messages may show up out of sync with standard output. This situation can be resolved, providing you know how to flush the stderr device buffer.

What a wonderful word, flush. It’s appropriate, and the action taken is like flushing your favorite porcelain seat: Contents are immediately released.

In C, you can flush any output stream. The effect is that any data waiting in the buffer is rushed out the door. This effect applies to open files as well as the stdout and stderr devices. The function is called fflush() and it’s defined in the stdio.h header file. Here’s the man page format:

int fflush(FILE *stream);

stream is a FILE pointer, which by definition can include the standard output and standard error devices, constants sdtout and stderr. Therefore, you can use the fflush() function to immediately dump error message output:

fprintf(stderr,"Something went horribly awry!\n");
fflush(stderr);

The effect of these two statements is to immediately output the message Something went horribly awry! to the console. This output doesn’t stop the program or alter any other behavior. The text is output regardless of any I/O redirection or filters.

Gander at the following code:

#include <stdio.h>

int main()
{
    const char normal[] = "Normal output ";
    const char error[] = "Error output ";

    fprintf(stdout,normal);
    fprintf(stderr,error);
    fprintf(stdout,normal);
    fprintf(stderr,error);
    fprintf(stdout,normal);
    fprintf(stderr,error);

    return(0);
}

A series of fprintf() statements output the two strings, one sent to the stdout device and the other to stderr. You would think the output would alternate, but here’s what the program coughs up:

Error output Error output Error output Normal output Normal output Normal output

All the output to the stderr device appears first, followed by all the output to stdout. It’s unexpected, but demonstrates the buffered nature of the stream.

I know two ways to fix the output. First, you can modify each string by appending a newline, '\n'. This character helps flush the output buffer, making the output alternate as you would expect. The problem with this solution is that it may not work for larger programs with a long runtime. Regardless, to guarantee instant output, use the fflush() function:

#include <stdio.h>

int main()
{
    const char normal[] = "Normal output ";
    const char error[] = "Error output ";

    fprintf(stdout,normal);
    fflush(stdout);
    fprintf(stderr,error);
    fflush(stderr);
    fprintf(stdout,normal);
    fflush(stdout);
    fprintf(stderr,error);
    fflush(stderr);
    fprintf(stdout,normal);
    fflush(stdout);
    fprintf(stderr,error);
    fflush(stderr);

    return(0);
}

After each fprintf() statement, the buffer is flushed. With this modification, the output appears as expected, forced that way thanks to fflush():

Normal output Error output Normal output Error output Normal output Error output

Some notes:

Using the fflush() function doesn’t close the output stream. The stdout and stderr streams are always open. For files, however, be aware that using fflush() won’t close the file; it must still be closed properly when you’re done writing data.

The fflush() function’s stream argument can be replace with NULL to flush all open output streams.

Also see this blog post on the setbuf() function.

6 thoughts on “Un-Buffering Output

  1. A while ago I tried out ncuses after buying your book and it is necessary to call refresh to flush all output to the screen. This makes sense as with curses you are likely to be building up a more complex UI than you would with the plain stdio functions, so writing to a buffer then showing it all in one go prevents flickering. At least I assume that’s the reason.

    Do you know if there’s a way of doing something similar with printf etc, ie preventing output being flushed until you call a certain function?

    The only way I can think of is writing to an in-memory buffer (string) then “puts()-ing” it in one go.

  2. Thanks for the reply.

    Can I make a suggestion please? A post on the restrict qualifier would be useful. I understand what it does but I’m not clear when you would or wouldn’t use it so I’d be interested in your views.

  3. I had a post on restrict scheduled, but I couldn’t find anything useful to demonstrate. I’ll look into it again.

  4. The reason I mentioned this is that I was using strftime the other day and noticed that 3 of its 4 arguments are restrict and I couldn’t be bothered to figure out why.

    Maybe you could write a post explaining why this and other standard library functions use restrict? Just a suggestion 🙂

  5. Thanks for the inspiration. The problem with restrict is that it’s a compiler optimization directive. So unless you decompile the code (which is a possibility), it’s difficult to demonstrate what it does.

    Obviously, whoever is coding the function knows about restrict and how to best use it.

Leave a Reply