Link to home
Start Free TrialLog in
Avatar of Ashim
Ashim

asked on

About the use/ need of fflush .....

Hello there,

Could any1 here tell me when exactly fflush is used?
I do understand that fflush is used to clear a stream ...like fflush(stdin) clears the buffer so functions like scanf / getchar can get the right input ?

If I say printf("\n");
         and then scanf("%c",&character)
then I should put fflush(stdin) to get the right input..... but do I have to uses fflush EACH TIME I use scanf ?? In that case, why did not the guy who designed C (Mr. Denis Ritchie was it ? ) make fflush a part of scanf ??



In case you find the above confusing,I rephrase it once again....

When EXACTLY do we have to use fflush ? Is it that we have to precede each scanf by an fflush(stdin) statement?

Thanx....
Avatar of dimitry
dimitry

Declaration:  int fflush(FILE *stream);
If the given stream has buffered output, fflush writes the output for stream to the associated file.
The stream remains open after fflush has executed. fflush has no effect on an unbuffered stream.

First of all stdin is only one type of stream. Any open
file can be flushed to be saved on disk. For speed OS usually has buffers for any stream output unless you
open file stream with special flag. This is the answer to
your question why Mr. Ritchi (if he even wrote once library funtions) didn't insert this into fscanf.

Usually you are using fflush(stdin) to be sure that to the
moment of scanf() call stdin stream is empty. If you don't
want this you don't flush it. If you will use next code:
  printf("Input number:");
  fflush(stdin);
  scanf("%d",&var);
then you will force user to enter "var" value after your
printf() output call. If you will use:
  printf("Input number:");
  scanf("%d",&var);
then "var" value can be enters before printf() and will be
taken without pause.

I hope I wasn't too long... :)
ASKER CERTIFIED SOLUTION
Avatar of F. Dominicus
F. Dominicus
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
sorry, I send the message before I was ready with it:
Use the given get_number like this:


enum {MAX_BUF=100}; // more than enough even for 64 bit long numbers
int main(void){
    int number = 0;
    int ret_val = 0;
   

    ret_val = get_number("Give me a number please", &number);
    if (0 == ret_val) {
          // error handling
    }
    // now you can use number here
   
    return 0;
}

So again. fflush(stdin) causes undefined behavior. Don't use fflush that way.

Perfect case in point of why fflush() is useful:

Last week we had a release-build-only crash in a lousy tool we inherited from someone else.  The tool has some pretty terrible code, so it's no big surprise that the bug can't be reproduced in Debug mode.  The program was shutting down seemingly at random, and we couldn't track down when/why.

We logged of all the major events in the application as they occurred.  Unfortunately, when the program would exit suddenly the last message would only be half-printed, or missing entirely.  The reason was that the (f)printf() being used for the log was getting called before the crash, but wasn't actually getting written out to the console.  Adding an fflush() just after the printf() ensured that every message got written out completely before program execution proceeded.

-sq
Dear fridom


I've rejected your proposed answer as Experts Exchange holds an experiment to work without the answer button.

See:        https://www.experts-exchange.com/jsp/communityNews.jsp
Paragraph: Site Update for Wednesday, November 06, 2002

By this rejection the Asker will be notified by mail and hopefully he will take his responsibility to finalize the question or post an additional comment.
The Asker sees a button beside every post which says "Accept This Comment As Answer" (including rejected answers) -- so if he/she thinks yours is the best, you'll be awarded the points and the grade.


EXPERTS: I will return in seven days to close this question. Please leave your thoughts and recommendations here


Thanks !

Jgould

Community Support Moderator
Experts Exchange
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

accept answer from fridom

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

mattjsimps
EE Cleanup Volunteer