They are quite different. They can both "flush" input, but in different senses of that word.
fflush
is a standard C function. Its behavior on input streams such as stdin
is undefined -- meaning that the C standard does not define its behavior.
Some systems do define the behavior of fflush
on an input stream. For example on Linux:
For input streams, fflush()
discards any buffered data that has been
fetched from the underlying file, but has not been consumed by the
application.
You can rely on fflush(stdin)
to behave in accordance with this description if your program runs on a Linux-based system, or on another system that documents the same behavior. Your program's behavior will not be portable; on other systems, it might behave in arbitrarily bad ways. (Most likely if fflush(stdin)
doesn't work, it will do nothing other than returning an error indication, but that's not guaranteed.)
Your own flushstdin
function does something different from the Linux behavior of fflush(stdin)
. It reads and discards all input functions up to the first newline or until EOF
(which is triggered either by end-of-file or by an error). It does this regardless of whether that input is buffered or not.
For example, suppose you type the chacters hello
(without a newline), then your program calls fflush(stdin)
, and then you enter a newline.
fflush(stdin)
, given the Linux documented behavior, will discard the hello
and return immediately, leaving the newline to be read by a later call. It "flushes" stdin
in the sense that it discards all pending input, regardless of what it is.
Your flushstdin()
function will read and discard the hello
and then wait until you type Enter (or Ctrl-D), and then read and discard that. It reads and discards all input up to a newline or EOF, regardless of whether it was pending at the time of the call.
And again, the behavior of fflush(stdin)
is not defined by the C standard, so using it makes your program non-portable (and your compiler will not necessarily warn you about that).
Incidentally, "that void stuff" is the definition of the flushstdin
function. It's not needed for fflush
because that's a standard C library function that's already defined for you.