Difference between fflush and setbuf

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

When to use and when not to use each?

 5
Author: LINQ, 2016-11-12

1 answers

fflush(stdin) it is mistakenly used to clear the default input buffer, but causes undefined behavior according to the language standard, so it should be avoided at any cost. I'd particularly never heard of setbuf(stdin, NULL), but apparently it disables stdin's buffer for the rest of the program, which seems to me a overkill for everyday situations.

If your intention is only to clear the input buffer before or after any operation (such as discarding remaining whitespace ) there are more canonical and secure alternatives, such as scanf(" ") (discards whitespace until first printable character).

 4
Author: Emoon, 2016-11-15 07:35:49