Use of fflush of function based on C language

  • 2020-04-02 00:56:07
  • OfStack

Fflush is used to empty the buffer flow, and although it is not generally felt, the default printf buffers the output. Fflush (stdout), make the stdout empty, will immediately output all the contents in the buffer. The example of fflush(stdout) may not be obvious, but it is obvious to stdin. Int a,c; The scanf (" % d ", & a); C = getchar (); So a=12,c= 'n' and: int a,c; The scanf (" % d ", & a); The fflush (stdin); C = getchar (); Input: 12 (enter) so a=12, c has not got the input value for the time being, so you need to input c again, because getchar is also the buffered input, '\n' is still in the buffer, but it has been cleared. Also fflush cannot act on redirected input streams. Fflush (stdin) flushes the standard input buffer and discards the contents of the input buffer
Fflush (stdout) flushes the standard output buffer and prints the contents of the output buffer to the standard output device

The fflush (stdout); // what does this sentence do?? - So fflush() does this : writes the contents of the output buffer to the file to which the pointer points if the parentheses are Pointers to the file to which the pointer is written, otherwise clears the output buffer. The stdout here is a pointer to the system-defined standard output file, which by default refers to the screen, where the contents of the buffer are written to the screen. But you can't tell from the code what the buffer will have, so it doesn't actually do anything

Related articles: