Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8532281
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T09:40:49+00:00 2026-06-11T09:40:49+00:00

Should fflush() not be used to flush a buffer even if it is an

  • 0

Should fflush() not be used to flush a buffer even if it is an output stream?

What is it useful for? How do we flush a buffer in general?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T09:40:50+00:00Added an answer on June 11, 2026 at 9:40 am

    Flushing the output buffers:

    printf("Buffered, will be flushed");
    fflush(stdout); // Prints to screen or whatever your standard out is
    

    or

    fprintf(fd, "Buffered, will be flushed");
    fflush(fd);  //Prints to a file
    

    Can be a very helpful technique. Why would you want to flush an output buffer? Usually when I do it, it’s because the code is crashing and I’m trying to debug something. The standard buffer will not print everytime you call printf() it waits until it’s full then dumps a bunch at once. So if you’re trying to check if you’re making it to a function call before a crash, it’s helpful to printf something like "got here!", and sometimes the buffer hasn’t been flushed before the crash happens and you can’t tell how far you’ve really gotten.

    Another time that it’s helpful, is in multi-process or multi-thread code. Again, the buffer doesn’t always flush on a call to a printf(), so if you want to know the true order of execution of multiple processes you should fflush the buffer after every print.

    I make a habit to do it, it saves me a lot of headache in debugging. The only downside I can think of to doing so is that printf() is an expensive operation (which is why it doesn’t by default flush the buffer).


    As far as flushing the input buffer (stdin), you should not do that. Flushing stdin is undefined behavior according to the C11 standard §7.21.5.2 part 2:

    If stream points to an output stream … the fflush function causes any unwritten data for that stream … to be written to the file; otherwise, the behavior is undefined.

    On some systems, Linux being one as you can see in the man page for fflush(), there’s a defined behavior but it’s system dependent so your code will not be portable.

    Now if you’re worried about garbage "stuck" in the input buffer you can use fpurge() on that.
    See here for more on fflush() and fpurge()

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Should we declare the private fields as volatile if the instanced are used in
Should timestamps always use UTC (as in 2012-06-14T10:32:11+00:00 ) and not local time (as
I tried flushing stdout with sys.stdout.flush() but it's still not working. It works fine
Not sure if this is the TCPClient or something else, I have used a
I do not understand all the mechanics surrounding Streams and even less around the
How to flush the stdin ?? Why is it not working in the following
Empirically it seems that flush() is not necessary after findAndUpdate() , I just couldn't
Should createUrl be called on controller or in views ? It doesn't matter? Or
Should I constantly have a session open in my web site with user accounts,
Should I consider the number of weeks in a month the number of mondays

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.