Dont use fflush(stdin) ..

Home Forums C-programming.. Dont use fflush(stdin) ..

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #2198 Reply
    Humble
    Keymaster

    lot many programmers and C programming books says about using fflush(stdin) to clear the input buffer.. Unfortunately its not correct.. You should not use fflush on stdin buffer.

    Lets look at the definition of fflush():

           fflush - flush a stream
    
           int fflush(FILE *stream);
    
     
     For output streams, fflush() forces a write
     of all user-space buffered data for the given output 
    or update stream via the stream's underlying write function. 
     For input streams, fflush() discards any buffered data
     that has been fetched from the underlying  file,  but 
     has  not been consumed by the application. 
     The open status of the stream is unaffected.
    
           If the stream argument is NULL, fflush() flushes
     all open output streams.
    
    
    
    
    Its defined  for output streams.. According to the standard, in posix system if you use fflush(stdin) you are into "undefined behavior" ..
    #2201 Reply
    Humble
    Keymaster
    This reply has been marked as private.
Viewing 2 posts - 1 through 2 (of 2 total)
Reply To: Dont use fflush(stdin) ..
Your information: