Difference between fflush and setbuf

Asked

Viewed 1,726 times

6

What is the difference between the functions fflush(stdin); and **setbuf(stdin, NULL);**?

When to use and when not to use each?

  • 1

    See if this helps: http://stackoverflow.com/questions/16780908/understanding-the-need-of-fflush-and-problems-associated-with-it

1 answer

4


fflush(stdin) is used erroneously to clear the default input buffer, but causes undefined behavior according to the standard of the language, therefore it should be avoided at any cost. Particularly had never heard of the setbuf(stdin, NULL), but apparently it disables the buffer stdin for the rest of the program, which seems to me a Overkill for everyday situations.

If your intention is just to clear the input buffer before or after any operation (such as discarding whitespace remnant) there are more canonical and safe alternatives, such as scanf(" ") (whitespace discard down to first printable character).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.