Thread: When and where to use fflush (stdout)?

  1. #1
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    715

    When and where to use fflush (stdout)?

    Hi,
    I found this simple code
    Code:
    #include <stdio.h>
    #include <ctype.h>
    #include <conio.h>
    
    int main ( void )
    {
      int yn = 'y';
    
      while ( tolower ( yn ) == 'y' ) {
        printf ( "Doing stuff\n" );
        printf ( "Do more stuff? (y/n): " );
        fflush ( stdout );/* this line */
        yn = getch();
        printf ( "\n" );
      }
    
      return 0;
    }
    What I noticed is fflush function call On an output stream, fflush causes any buffered but unwritten data to be written;
    I cannot figure out why fflush (stdout) is called here in this code
    I try to comment this line and behavior was exactly the same.
    Can someone explain what is purpose of calling fflush (stdout) for output stream here and general?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > I cannot figure out why fflush (stdout) is called here in this code
    > I try to comment this line and behavior was exactly the same.
    Because you're not guaranteed to see previous printf() output if that output doesn't end in a newline.

    Basically, you only need it if you're displaying say a prompt without a newline, and you want to make sure the user can see it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    715
    Quote Originally Posted by Salem
    Because you're not guaranteed to see previous printf() output if that output doesn't end in a newline.

    Basically, you only need it if you're displaying say a prompt without a newline, and you want to make sure the user can see it.

    Thanks Salem, but if you could give me more details.
    For instance if I have two printf like this

    Code:
    	printf("1.bla bla");
    	printf("2.bla bla);
    Does that mean that user is not guaranteed to see first printf output because there is no new line character??? Probably I didn't understand your post.

    And for that code in my first post is
    Code:
    	fflush (stdout);
    necessary or not.

    I'm using Bloodshed Dev-C++, so is there any code example that will help me to really understand the significance of fflush (stdout).

    In other words when there are character left in an output stream?

  4. #4
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Code:
        printf("1.bla bla");
        printf("2.bla bla);
    In this example its not guaranteed that either will be written to the screen immediatly. However they will be written to the buffer, and when they are displayed to the screen will be written in order

    Trying running this code:
    Code:
    unsigned i,j;
    for (i=0; i < UINT_MAX; i++)
      for (j=0; j < UINT_MAX; j++)
        putchar('.');
    And see what happens. Then try this:
    Code:
    unsigned i,j;
    for (i=0; i < UINT_MAX; i++)
      for (j=0; j < UINT_MAX; j++)
      {
        putchar('.');
        fflush(stdout);
      }
    And see what you get.

  5. #5
    .
    Join Date
    Nov 2003
    Posts
    307
    If you send a line of text plus a trailing \n to the screen using printf the line will appear on the screen.

    If the line of text does not end in a \n, there is no guarantee that any of the text will appear on the screen. fflush() moves everything that is "pending" onto the screen.

    fflush(stdout) means write all of the buffered data to it's destination - whatever stdout is.

  6. #6
    Registered User computerfreaks's Avatar
    Join Date
    Jan 2005
    Posts
    30
    I personally use the fflush before a getc command... the reason for this is that sometimes there is buffered text that causes the program to skip the getc command.

    It has been a common problem for me, and the only fix was to use fflush

  7. #7
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Quote Originally Posted by computerfreaks
    I personally use the fflush before a getc command... the reason for this is that sometimes there is buffered text that causes the program to skip the getc command.

    It has been a common problem for me, and the only fix was to use fflush
    You must be referring to using fflush() with stdin which is a bad thing to do. There's an FAQ subject on it.
    If you understand what you're doing, you're not learning anything.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    No, you're using it wrong. You're thinking of "fflush( stdin )" which is wrong and shouldn't be used.

    [edit] Curses, foiled again! [/edit]

    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Registered User computerfreaks's Avatar
    Join Date
    Jan 2005
    Posts
    30
    ooppps, will go and read the FAQ now.

    thanks

Popular pages Recent additions subscribe to a feed