20

I have the following program

#include <stdio.h>
#include <stdlib.h>

int main()
{
   char ans[8];
    int i;
    for(i=1;i<=3;i++)
    {
        printf("\n What is the unit of traffic ?");
        scanf("%s",ans);
        fflush(stdin);

        if(stricmp(ans,"Earlang")==0)
        {
            printf("\nAnswer is correct");
            exit(1);
        }
        else
            if(i<3)
            printf("\n Try Again!\n");
    }
    printf("\n Nunit of traffic is Earlang:");
}

What is the use of fflush(stdin) in this program?

Roberto Caboni
  • 7,252
  • 10
  • 25
  • 39
Mahi
  • 553
  • 2
  • 7
  • 23
  • 5
    @MitchWheat As you (should) know, a lot of docs will not mention `fflush(stdin)`, since it only has a defined behavior on some platforms. It's perfectly normal to be puzzled by this at first (but there must be a ton of duplicate questions on SO, e.g. [Using fflush(stdin)](http://stackoverflow.com/questions/2979209/using-fflushstdin)). – Gilles 'SO- stop being evil' Aug 18 '13 at 09:01
  • https://stackoverflow.com/questions/2979209/using-fflushstdin – M.M Jul 24 '18 at 00:52

3 Answers3

27

It's not in standard C, so the behavior is undefined.

Some implementation uses it to clear stdin buffer.

From C11 7.21.5.2 The fflush function, fflush works only with output/update stream, not input stream.

If stream points to an output stream or an update stream in which the most recent operation was not input, the fflush function causes any unwritten data for that stream to be delivered to the host environment to be written to the file; otherwise, the behavior is undefined.

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
  • 8
    +1. This is perfectly right, from the view of the C standard. Some implemetations give semantics to `fflush()`ing an input stream, but that is an extension. – glglgl Aug 11 '13 at 09:20
11

it clears stdin buffer before reading. From the man page:

For output streams, fflush() forces a write of all user-space buffered data for the given output or update stream via the stream's underlying write function. For input streams, fflush() discards any buffered data that has been fetched from the underlying file, but has not been consumed by the application.

Note: This is Linux-specific, using fflush() on input streams is undefined by the standard, however, most implementations behave the same as in Linux.

iabdalkader
  • 17,009
  • 4
  • 47
  • 74
  • in the above program , even though fflush(stdin) is not written , the program will execute , but how fflush(stdin) will be helpfull.please suggest. – Mahi Aug 11 '13 at 09:15
  • @user2526830 I'm not sure what you're saying, but like I said it clears the buffer before reading again. – iabdalkader Aug 11 '13 at 09:17
  • 1
    @user2526830 It will make sure that the program always executes properly on different machines, running different processes, under different system loads and NOT just on your machine. – TheCodeArtist Aug 11 '13 at 09:18
  • 1
    The man page is actually a bit misleading: it describes the (POSIX-mandated, not just Linux-specific) behavior for *seekable* input streams. The Linux `fflush` does exactly nothing when called on stdin (well, it attempts an `lseek`, catches `ESPIPE`, and then does exactly nothing) – Cubbi Mar 17 '15 at 02:40
6

It's an unportable way to remove all data from the input buffer till the next newline. I've seen it used in cases like that:

char c;
char s[32];
puts("Type a char");
c=getchar();
fflush(stdin);
puts("Type a string");
fgets(s,32,stdin);

Without the fflush(), if you type a character, say "a", and the hit enter, the input buffer contains "a\n", the getchar() peeks the "a", but the "\n" remains in the buffer, so the next fgets() will find it and return an empty string without even waiting for user input.

However, note that this use of fflush() is unportable. I've tested right now on a Linux machine, and it does not work, for example.

user2671945
  • 315
  • 1
  • 6
  • 1
    In linux use: #include void clean_stdin(void) { int c; do { c = getchar(); } while (c != '\n' && c != EOF); } https://stackoverflow.com/a/17319153/1925162 – Sunny127 Dec 21 '17 at 08:38