First look at the following program:
650) this.width=650; "src=" http://s2.51cto.com/wyfs02/M01/84/7D/wKiom1eR3_ejDQ02AAAuFXiQAbY913.png "title=" Buffer 1. PNG "width=" height= "border=" 0 "hspace=" 0 "vspace=" 0 "style=" width:300px;height:120px; "alt=" wkiom1er3_ Ejdq02aaaufxiqaby913.png "/>
In the above program, the printf function prints a string that finally did not have a newline character, and finally called the _exit () function, which resulted in the last not printing out Hello world;
650) this.width=650; "src=" Http://img.baidu.com/hi/jx2/j_0017.gif "alt=" j_0017.gif "/> What's this about???
Here are some of the buffering mechanisms:
1, full buffer . Full buffering refers to the fact that the system does not perform actual IO operations until the standard IO buffers are filled, and note that for files residing on disk, the standard IO Library is generally used for full buffering. Call the Fflush function to flush a stream. Flushing means writing the contents of the buffer to disk.
2, row buffer . In this case, the standard IO performs an IO operation when a newline character is encountered in the input and output, and note that the row buffer is usually used when the stream touches the terminal.
3, no buffering . Unbuffered means that the standard IO library does not buffer the characters; Note that standard error stream stderr is usually unbuffered.
Then we introduce several exit functions:
1, exit (). After calling the Exit function, it first performs a series of cleanup processes, including calling the execution of each termination handler, shutting down all standard IO streams, and then entering the kernel.
2, _exit (). Unlike exit, it goes directly into the kernel without cleaning up the work. This function is explained by posix.1 and placed inside the unistd.h.
3, _exit (). Similarly, it does not perform cleanup work and goes directly into the kernel. This function is described by ISO C as exit and placed inside the stdlib.h.
now again Looking at the code above, it is found that printf () is a row buffer, and the printed string does not have a newline character, so there is no actual I/O operation until he encounters a newline character or does not fill the buffer, and the next _exit function immediately enters the kernel without processing i/ o Buffer, so we do not see the Hello World statement on the terminal.
650) this.width=650; "src=" Http://img.baidu.com/hi/youa/y_0036.gif "alt=" y_0036.gif "/> Fix this code:
1. Add a newline character after the string, at which point a newline character is encountered and the IO operation is performed
2. Call the exit () function, the Exit function cleans up the data in the buffer before entering the kernel, and then closes the IO stream
3. The Fclose function can be called to achieve the purpose, The fclose function implicitly contains a fflush operation that puts the buffer in the data is flushed to the terminal.
#include <stdio.h>int main () {printf ("Hello world!"); Fclose (stdout); _exit (0);}
This article is from the "777 Fast" blog, be sure to keep this source http://10324228.blog.51cto.com/10314228/1828883
Buffered and unbuffered