Difference between fflush and setbuf

5

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

When to use and when not to use each?

    
asked by anonymous 12.11.2016 / 15:33

1 answer

4

fflush(stdin) is used erroneously to clear the default input buffer, but causes undefined behavior according to the language pattern, so it should be avoided at all costs. I've never particularly heard of setbuf(stdin, NULL) , but apparently it disables the buffer of stdin for the rest of the program, which seems like an overkill for everyday situations. p>

If you just want to clear the input buffer before or after any operation (such as dropping whitespace ), there are more canonical and secure alternatives, such as scanf(" ") ( discards whitespace up to the first printable character).

    
12.11.2016 / 17:35