Hans Dulimarta

Unbuffered I/O

One student sent me the following email and my response is posted below his email.

Dr. Dulimarta,

Greetings! I am in your EGR261 class this summer. I wanted to let you know I searched online and found a great fix for the printf issue we’ve been having with Eclipse in Windows.

The issue is automatically resolved when these two lines are added at the beginning of the main function:

setvbuf(stdout, NULL, _IONBF, 0);
setvbuf(stderr, NULL, _IONBF, 0);

The nice thing about this method is that it does not require the fflush line after every printf for it to show in the console. Perhaps you’d like to add it to your arsenal of fixes for this problem, if it wasn’t already on your radar.

I hope you are having a great week. See you in class Thursday!


I just want to make sure you know when [not] to use the above technique. The two calls above disable output buffering on stdout and stderr.

I/O operations are several order of magnitude slower than other operations on your computer. To improve the performance, we use buffers to temporarily hold the data before they are actually delivered to the output device.

Let’s say your program is about to print a 413-character message to stdout.

  • Without using an output buffer, your program will perform 413 I/O calls to print the message one character at a time.
  • Using an output buffer, your program will perform only ONE I/O call to transfer the entire 413 characters as a group.