printf function and buffer

printf function and buffer

PR INTF function is a function of the line buffer, first write the contents of the buffer, after certain conditions are met, only the contents written into the corresponding file or stream.

The basic conditions are as follows:

1 . Buffer fills
 2 . Has written the character '\ n-' ' \ R & lt ' 
. 3 . Fflush call or manually refresh buffer stdout
 . 4 when. Scanf call from the buffer to read data and the like, will also refresh data in the buffer 
5. end of program

Example 1 The following verification:

. 1 #include <stdio.h>
 2 #include <stdlib.h>
 . 3  
. 4  / * argc: number of input parameters of the command line, argv: command line parameters 
 . 5  * to the argv array of character pointers, argv [i] is the i-th point command-line argument pointer contents
 . 6   * / 
. 7  int main ( int argc, char ** the argv) { 
 . 8      int I;
 . 9      char a = ' a ' ;
 10      IF (! argc = 2 ) // command line parameters 2 otherwise error 
. 11      {
 12 is          the printf ( " the Usage: Number The% S \ n- " , the argv [0 ]); 
 13 is          return  0 ;
 14      }
 15  
16      for (I = 0 ; I <atoi (the argv [ . 1 ]); I ++) // atoi: character into an integer 
. 17      {
 18 is          the printf ( " % C " , A) ;
 19      }
 20      
21      the while ( 1 );   // let the program has been running 
22 }

operation result:

 Explanation of linux, printf buffer size is 1024 bytes. while (1) the program has been run, when the buffer is full, the output will not print.

Example 2:

. 1 #include <stdio.h>
 2  
. 3  int main ()
 . 4  {
 . 5      int I;
 . 6      for (I = 0 ; I < 10 ; I ++ )
 . 7      {
 . 8          the printf ( " A " );
 . 9          fflush (stdout); / / parameters for the file pointer 
10      }
 . 11      the while ( . 1 );
 12 is      return  0 ;
 13 is }

Not before streaming refresh

After the stream Refresh fflush

Guess you like

Origin www.cnblogs.com/y4247464/p/12024742.html