Question

I am a beginner in ANSI C, and I have a question, it may be silly question and I am sorry for it.

#include<stdio.h>
    main()
    {
          int age;
          printf("Hello World!\n");
          printf("Please enter your age: ");
          scanf("%d", &age);
          printf("You entered %d\n", age);
          fflush(stdin);
          getchar();
    }

It is my second program to learn scanf function. My question is : I know that printf, scanf, fflush, stdin, and getchar are defined in stdio.h but only when I use fflush(stdin) I must put #include<stdio.h>, but when use any other method I can remove that line #include.

Was it helpful?

Solution

You must have #include <stdio.h> when you call any function declared in that header.

Ok, that's not quite true. In the 1989/1990 version of the language standard, a call can create an implicit declaration of a function. If that happens to match the correct declaration, you can get away with it;. Otherwise your program's behavior is undefined -- which means you still might get away with it, or not, but the compiler isn't required to warn you about it. Since printf takes a variable number of arguments, you must have a visible declaration to avoid undefined behavior -- and the way to get a visible declaration is #include <stdio.h>.

(You can also declare the function yourself, but that's error-prone, and there's no good reason to do that.)

In C99 and later, there are no implicit declarations.

main() should be int main(void).

fflush(stdin) has undefined behavior. If you want to discard characters entered after the scanf() call, you can read and discard them.

OTHER TIPS

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).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top