How to turn off buffering of stdout in C

I want to turn off the buffering for the stdout for getting the exact result for the following code

while(1) {
printf(".");
sleep(1);
}

The code printf bunch of '.' only when buffer gets filled.


Solution 1:

You can use the setvbuf function:

setvbuf(stdout, NULL, _IONBF, 0);

The link above has been broken. Here're another links to the function.

  • POSIX

  • C/C++

Solution 2:

You can also use setbuf

setbuf(stdout, NULL);

This will take care of everything