How to turn off buffering of stdout in C
You can also use setbuf
setbuf(stdout, NULL);
This will take care of everything
You can use the setvbuf function:
setvbuf(stdout, NULL, _IONBF, 0);
Here're some other links to the function.
POSIX
C/C++