1

In the following code, I try to let fflush() to clear the standard input, but when I ran the code, I noticed after the first iteration, it will skip the getchar() part. I read some man page about fflush(). But I couldn't find anything about why fflush() didn't clear the buffer in this case. In first iteration, I typed y after "puts("Prees n to quit"), but in the second iteration getchar() still get y from the buffer.

int main(void)
{
    char month_input[size];
    typedef struct date_info user_input;
    user_input info;
    _Bool check = true;
    char ch;

    while(check)
    {   
        puts("Press n to quit");
        printf("ch = %d\n", ch);

        if((ch = getchar()) == 'n')
            break;
        fflush(stdin);

        printf("which month?\n");
        scanf("%s", month_input);
        info.month = month_input;

        printf("Enter the date\n");
        scanf("%d", &info.date);

        printf("Enter the year\n");
        scanf("%d", &info.year);


        fflush(stdin);


    }
    printf("month = %s / date = %d / year = %d", info.month, info.date, info.year);

    return 0;
}
Yu Jia
  • 21
  • 5
  • 4
    `fflush(stdin);` is undefined behaviour. Google that. Check the return value from `scanf` and take appropriate action - i.e. eat the input until end of line – Ed Heal Jan 09 '18 at 04:31
  • I google that. Here is a quote that I found "For input streams, fflush() discards any buffered data that has been fetched from the underlying file, but has not been consumed by the application." According to this quote, whether it means '\r' will not be clear because it is not buffered data has been fetched from the underlying file. it is the buffered data still remain in the standard input stream? – Yu Jia Jan 09 '18 at 04:50
  • 2
    Which platform are you on? See [Using `fflush(stdin)`](https://stackoverflow.com/questions/2979209/) for limitations — it only works on Windows, at best. And it depends on which runtime library you use too. – Jonathan Leffler Jan 09 '18 at 04:57
  • Linux and using gcc to compile – Yu Jia Jan 09 '18 at 04:59
  • Please check the return values from `scanf` – Ed Heal Jan 09 '18 at 05:00
  • I did notice on some Q&A sources, some people said fflush(stdin) will work – Yu Jia Jan 09 '18 at 05:00
  • 1
    The behavior of `fflush(stdin)` is not defined by the C standard. `fflush()` is for flushing *output* from the C library's buffers to their destination. The conforming way to discard *input* is to read it (and ignore what you read). – John Bollinger Jan 09 '18 at 05:02
  • Or if you don't believe us, then go pose your question on whatever Q&A site told you that `fflush(stdin)` would work. In the remote chance that it was *here*, please do give us links, and we'll take care of it. – John Bollinger Jan 09 '18 at 05:06
  • 1
    I think you guys are right. I go back recheck and reread parts of C primer plus in Chapter 13. It does say "The effect of using fflush() on an input stream is undefined." My bad. – Yu Jia Jan 09 '18 at 05:13

0 Answers0