This article was reproduced from: http://blog.chinaunix.net/uid-30058258-id-5029847.html
printf is a row buffer function
The printf function is a standard function and will eventually be called to the system call function, and the printf function will fall into the write system function, but calling the write function requires satisfying some conditional pieces.
(1) Buffer full
(2) encountered \ r \ n
(3) Call the Fflush function to flush the buffer
(4) scanf reads the contents of the buffer
(5) The process or thread ending calling the printf function calls the Fflush function
The size of the detection buffer is 1024B.
Fflush function: Write out all the data not written in the file stream.
Function name: Fflush functions: Clear read-write buffer, need to immediately put the output buffer data to physically write when the header file: stdio.h prototype: int fflush (file *stream) where stream is the stream to be flushed
Fflush (stdin) refreshes the standard input buffer, discards the input buffer [non-standard]fflush (STDOUT) refreshes the standard output buffer, prints the output buffer to the standard output device printf ("...). "), with Fflush (stdout), to improve printing efficiency
Example for (i=1;i<=10;i++)
{
printf ("%d", I);
Sleep (1);
}
This is not output a number per second but 10 seconds after the output of 10 digits! The contents of the buffer are all output after printf plus fflush.
fflush function and printf function "Go" in Linux