Thread: fflush(stdout)

  1. #1
    Registered User
    Join Date
    Dec 2007


    Do I need to use:
    Each time I print something to the screen (printf, puts, putchar.....) ?
    If So why?

    Many thanks !!!

  2. #2
    Registered User
    Join Date
    Sep 2006
    I've seen this in some code in books, even.

    On my two compilers, stdout is unbuffered, so there's nothing to fflush() out. I've used it, for fun, and never seen a bit of difference.

    I never even try it, anymore. It does nothing for me. YMMV

  3. #3
    Join Date
    Oct 2007
    Inside my computer
    Unless you use a very old compiler, then no.
    If the buffer needs to be flushed, the libraries will do it for you.
    Otherwise you could just test.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Farncombe, Surrey, England
    Like Elysia said, it depends on the compiler (or perhaps more accurately, the runtime library that the compiler is using). printf(), etc, are not GUARANTEED to write the data out immediately. Most runtime libraries flush when you switch to reading input, and often also whenever there is a newline in the output, so most code will work just fine without.

    If you are writing something like a progress measure [on the same line over and over, or one "dot" at a time], then it's most likely a good idea to put a fflush(stdout) after each update.

    In older runtime libraries, you may also need a fflush() when reading data immediately after writing something without a newline, e.g.
       printf("Enter your name:")
       fgets(name, sizeof(name), stdin);
    could end up reading the name without having shown the prompt for the name - which make the app look like it's hung, and you don't realize it's waiting for input... Turbo C typically needed this.

    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    >Each time I print something to the screen (printf, puts, putchar.....) ?
    As with most answers, it depends. You don't need to force a flush if the last character printed was a newline, such as with puts. You don't need to force a flush if it isn't needed, but the usual issue is this one:
    printf ( "Enter some input: " );
    fgets ( input, sizeof input, stdin );
    The implementation could choose to block for input before flushing stdout's buffer. In that case, the user would see a blank screen and not know what to do. I haven't seen this happen on Windows systems, but I have seen it on Unix, so it's not a theoretical issue. Forcing a flush guarantees that the prompt will be visible before blocking for input:
    printf ( "Enter some input: " );
    fflush ( stdout );
    fgets ( input, sizeof input, stdin );
    I'd recommend using fflush in such cases for portability reasons. Not to mention extra l33t points for knowing obscure problems and solutions in the standard stream library.

    >If the buffer needs to be flushed, the libraries will do it for you.
    Yes, but the library's need and the programmer's need are two different things. The library is only required to flush a buffered output stream when the buffer becomes full, a newline is written, or fflush is called. Relying on the library doesn't guarantee a solution to the above problem unless you get lucky and the buffer fills up with your prompt.

    Implementations can choose to work around the problem for programmer convenience, but it's not a requirement. It's easier not to apply a workaround, so you shouldn't expect your implementation to do so.
    My best code is written with the delete key.

  6. #6
    Registered User
    Join Date
    Dec 2007
    Thanks a million

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fflush(stdout)
    By ssharish2005 in forum C Programming
    Replies: 6
    Last Post: 05-31-2009, 01:36 PM
  2. Replies: 8
    Last Post: 12-21-2008, 09:42 PM
  3. while statement
    By wonderpoop in forum C Programming
    Replies: 13
    Last Post: 10-23-2006, 09:14 PM
  4. custom header file can someone help?
    By shaftinafrica in forum C Programming
    Replies: 16
    Last Post: 11-15-2005, 09:09 PM
  5. Animation not working....
    By aquinn in forum C Programming
    Replies: 7
    Last Post: 02-19-2005, 05:37 AM