You probably need that header file so stdin
will be defined. If you have compiler warnings turned off, the compiler will not let you know about undefined functions. (If they really are
undefined, you'll get a linker error later on). stdin
is a variable of type FILE *
, not a function.
Please do not call fflush(stdin)
. It has undefined behaviour. If you need to clear the input buffer, use another method. The standard way to do this is to attempt to read data until the end of the next line:
int flush_line(FILE *file)
{
int ch;
do {
ch = fgetc(file);
} while (ch != '\n' && ch != EOF);
if (ch == EOF && ferror(file)) {
return -1;
}
return 0;
}
And then you would call flush_line(stdin)
.
If portability is not a concern (note that this will not work with Windows), you could temporarily make the file descriptor non-blocking and read all input from it:
int flush_in(FILE *file)
{
int ch;
int flags;
int fd;
fd = fileno(file);
flags = fcntl(fd, F_GETFL, 0);
if (flags < 0) {
return -1;
}
if (fcntl(fd, F_SETFL, flags | O_NONBLOCK)) {
return -1;
}
do {
ch = fgetc(file);
} while (ch != EOF);
clearerr(file);
if (fcntl(fd, F_SETFL, flags)) {
return -1;
}
return 0;
}
And then you would call flush_in(stdin)
.