Replacement of fflush(stdin)

This is well explained in the C FAQ. See also: explanation. The proposed solutions:

  • Quit using scanf. Use fgets and the sscanf
  • Use this to eat the newline

    while((c = getchar()) != '\n' && c != EOF)
    /* discard the character */;

The fact that flushing stdin works on some implementations is wrong.

Some vendors do implement fflush so
that fflush(stdin) discards unread
characters, although portable programs
cannot depend on this.

Leave a Comment