81

So a quick Google search for fflush(stdin) for clearing the input buffer reveals numerous websites warning against using it. And yet that's exactly how my CS professor taught the class to do it.

How bad is using fflush(stdin)? Should I really abstain from using it, even though my professor is using it and it seems to work flawlessly?

Lundin
  • 155,020
  • 33
  • 213
  • 341
wrongusername
  • 16,698
  • 33
  • 118
  • 201
  • 8
    http://www.codinghorror.com/blog/2007/03/the-works-on-my-machine-certification-program.html – BlueRaja - Danny Pflughoeft Jun 05 '10 at 04:51
  • 9
    Both [Windows](http://msdn.microsoft.com/en-us/library/9yky46tz.aspx) and [Linux](http://linux.die.net/man/3/fflush) define the behaviour of `fflush()` on an input stream, and even define it the same way (miracle of miracles). The POSIX, C and C++ standards for [`fflush()`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/fflush.html) do not define the behaviour, but none of them prevent a system from defining it. If you're coding for maximum portability, avoid `fflush(stdin)`; if you're coding for platforms that define the behaviour, use it — but be aware that it is not portable. – Jonathan Leffler Dec 22 '13 at 22:13
  • Cygwin is an example of a fairly common platform on which `fflush(stdin);` does not clear the input. – M.M Sep 28 '14 at 02:41
  • 3
    It also depends on exactly what you expect `fflush(stdin)` to do. – Keith Thompson Dec 13 '15 at 03:54
  • @JonathanLeffler The windows doc says`If the stream was opened in read mode, or if the stream has no buffer, the call to fflush has no effect, and any buffer is retained`, and the linux doc says `For input streams, fflush() discards any buffered data that has been fetched from the underlying file, but has not been consumed by the application.` That's not exactly the same way, windows retains the buffer, and linux discards the buffer. – ssbssa Aug 17 '20 at 10:26

6 Answers6

78

Simple: this is undefined behavior, since fflush is meant to be called on an output stream. This is an excerpt from the C standard:

int fflush(FILE *ostream);

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

So it's not a question of "how bad" this is. fflush(stdin) is plainly wrong, and you mustn't use it, ever.

Eli Bendersky
  • 231,995
  • 78
  • 333
  • 394
  • 1
    In his defense, knowing what is and isn't "undefined behavior" basically means having the whole C specification memorized... `fflush(stdin)` is actually a very common mistake. – BlueRaja - Danny Pflughoeft Jun 05 '10 at 05:00
  • 16
    @BlueRaja: there's defense for a newbie mistake here, but **no defense for a teacher** propagating wrong knowledge! Any reference of `fflush` makes clear it's meant for output streams right in the first paragraph, you don't have to memorize the C standard for that! – Eli Bendersky Jun 05 '10 at 05:04
  • 6
    @Eli: No one can know everything. The processor will never know his mistake until someone tells him... I used `fflush(stdin)` for years until I discovered it's UB (by accident) – BlueRaja - Danny Pflughoeft Jun 05 '10 at 05:06
  • 4
    Err, shouldn't one normally consult the documentation for a function before they use it? Especially a professor? – Alex Budovski Jun 05 '10 at 05:28
  • @EliBendersky So it's safe to use it with output streams?! – mrk Sep 16 '12 at 16:00
  • 6
    Another point of defense would be the following part of the man page (various glibc versions on Linux): "For input streams, `fflush()` discards any buffered data that has been fetched from the underlying file, but has not been consumed by the application. The open status of the stream is unaffected." Although it's UB, some implementations seem to make guarantees without mentioning its status with respect to the standard. – Daniel Fischer Oct 29 '12 at 20:23
  • 2
    @DanielFischer: Under the "CONFORMING TO" section of the glibc man page: "The standards do not specify the behavior for input streams." That's a good section to read sometimes. :) So even glibc mentions that it's not standard behavior, and even that it is undefined. So it shouldn't be used in order to be standard-compliant/portable/what-have-you. – Victor Zamanian Feb 23 '13 at 00:57
  • 3
    @VictorZamanian: it depends on what you mean by 'undefined behaviour'. On Linux, the manual defines the behaviour; using `fflush(stdin)` is defined behaviour on Linux. It is not defined according to the standards (so it does not conform to any standard-mandated behaviour in this instance), but it is defined by Linux (or, more accurately, by glibc). – Jonathan Leffler Feb 17 '14 at 15:29
  • @JonathanLeffler I am of course referring to the standard, not *implementation-defined behavior* as Linux's and glibc's behaviors are. – Victor Zamanian Feb 17 '14 at 20:27
  • 4
    There's another aspect that I seldom see mentioned: `fflush(stdin)` is much worse than just implementation-defined behavior. **Even if it did work as most people intend to, it would terrible.** Imagine if stdin is not someone dumbly typing input, but came from another program or shell redirection: it would read the beginning of the file and then just erase the rest. It is really dumb to think that stdin is always something so slow as a human operator. – Rafael Lerm Jan 17 '15 at 23:14
  • I'm downvoting this because it's basically saying that you are not allowed to use compiler extensions. – klutt Jan 25 '21 at 16:11
43

Converting comments into an answer — and extending them since the issue reappears periodically.

Standard C and POSIX leave fflush(stdin) as undefined behaviour

The POSIX, C and C++ standards for fflush() explicitly state that the behaviour is undefined, but none of them prevent a system from defining it.

ISO/IEC 9899:2011 — the C11 Standard — says:

§7.21.5.2 The fflush function

¶2 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.

POSIX mostly defers to the C standard but it does mark this text as a C extension.

[CX] For a stream open for reading, if the file is not already at EOF, and the file is one capable of seeking, the file offset of the underlying open file description shall be set to the file position of the stream, and any characters pushed back onto the stream by ungetc() or ungetwc() that have not subsequently been read from the stream shall be discarded (without further changing the file offset).

Note that terminals are not capable of seeking; neither are pipes or sockets.

Microsoft defines the behaviour of fflush(stdin)

Microsoft and the Visual Studio runtime defines the define the behaviour of fflush() on an input stream.

If the stream is open for input, fflush clears the contents of the buffer.

M.M notes:

Cygwin is an example of a fairly common platform on which fflush(stdin) does not clear the input.

This is why this answer version of my comment notes 'Microsoft and the Visual Studio runtime' — if you use a non-Microsoft C runtime library, the behaviour you see depends on that library.

Linux documentation and practice seem to contradict each other

Surprisingly, Linux nominally documents the behaviour of fflush(stdin) too, and even defines it the same way (miracle of miracles).

For input streams, fflush() discards any buffered data that has been fetched from the underlying file, but has not been consumed by the application.

I remain a bit puzzled and surprised at the Linux documentation saying that fflush(stdin) will work. Despite that suggestion, it most usually does not work on Linux. I just checked the documentation on Ubuntu 14.04 LTS; it says what is quoted above, but empirically, it does not work — at least when the input stream is a non-seekable device such as a terminal.

demo-fflush.c

#include <stdio.h>

int main(void)
{
    int c;
    if ((c = getchar()) != EOF)
    {
        printf("Got %c; enter some new data\n", c);
        fflush(stdin);
    }
    if ((c = getchar()) != EOF)
        printf("Got %c\n", c);

    return 0;
}

Example output

$ ./demo-fflush
Alliteration
Got A; enter some new data
Got l
$

This output was obtained on both Ubuntu 14.04 LTS and Mac OS X 10.11.2. To my understanding, it contradicts what the Linux manual says. If the fflush(stdin) operation worked, I would have to type a new line of text to get information for the second getchar() to read.

Given what the POSIX standard says, maybe a better demonstration is needed, and the Linux documentation should be clarified.

demo-fflush2.c

#include <stdio.h>

int main(void)
{
    int c;
    if ((c = getchar()) != EOF)
    {
        printf("Got %c\n", c);
        ungetc('B', stdin);
        ungetc('Z', stdin);
        if ((c = getchar()) == EOF)
        {
            fprintf(stderr, "Huh?!\n");
            return 1;
        }
        printf("Got %c after ungetc()\n", c);
        fflush(stdin);
    }
    if ((c = getchar()) != EOF)
        printf("Got %c\n", c);

    return 0;
}

Example output

Note that /etc/passwd is a seekable file. On Ubuntu, the first line looks like:

root:x:0:0:root:/root:/bin/bash

On Mac OS X, the first 4 lines look like:

##
# User Database
# 
# Note that this file is consulted directly only when the system is running

In other words, there is commentary at the top of the Mac OS X /etc/passwd file. The non-comment lines conform to the normal layout, so the root entry is:

root:*:0:0:System Administrator:/var/root:/bin/sh

Ubuntu 14.04 LTS:

$ ./demo-fflush2 < /etc/passwd
Got r
Got Z after ungetc()
Got o
$ ./demo-fflush2
Allotrope
Got A
Got Z after ungetc()
Got B
$

Mac OS X 10.11.2:

$ ./demo-fflush2 < /etc/passwd
Got #
Got Z after ungetc()
Got B
$

The Mac OS X behaviour ignores (or at least seems to ignore) the fflush(stdin) (thus not following POSIX on this issue). The Linux behaviour corresponds to the documented POSIX behaviour, but the POSIX specification is far more careful in what it says — it specifies a file capable of seeking, but terminals, of course, do not support seeking. It is also much less useful than the Microsoft specification.

Summary

Microsoft documents the behaviour of fflush(stdin). Apparently, it works as documented on the Windows platform, using the native Windows compiler and C runtime support libraries.

Despite documentation to the contrary, it does not work on Linux when the standard input is a terminal, but it seems to follow the POSIX specification which is far more carefully worded. According to the C standard, the behaviour of fflush(stdin) is undefined. POSIX adds the qualifier 'unless the input file is seekable', which a terminal is not. The behaviour is not the same as Microsoft's.

Consequently, portable code does not use fflush(stdin). Code that is tied to Microsoft's platform may use it and it will work, but beware the portability issues.

POSIX way to discard unread terminal input from a file descriptor

The POSIX standard way to discard unread information from a terminal file descriptor (as opposed to a file stream like stdin) is illustrated at How can I flush unread data from a tty input queue on a Unix system. However, that is operating below the standard I/O library level.

Community
  • 1
  • 1
Jonathan Leffler
  • 666,971
  • 126
  • 813
  • 1,185
  • That is probably the better answer in the context of what the OP asked for, although the accepted one is not wrong. To show clearly that it isn´t standard-compliant on one side, but showing that it might be right used on a specific implementation on the other. +1 – RobertS supports Monica Cellio May 10 '20 at 08:02
22

According to the standard, fflush can only be used with output buffers, and obviously stdin isn't one. However, some standard C libraries provide the use of fflush(stdin) as an extension. In that case you can use it, but it will affect portability, so you will no longer be able to use any standards-compliant standard C library on earth and expect the same results.

S.S. Anne
  • 13,819
  • 7
  • 31
  • 62
tiftik
  • 908
  • 5
  • 10
8

I believe you should never call fflush(stdin), for the simple reason that you should never even find it necessary to try to flush input in the first place. Realistically, there is only one reason you might think you had to, and that is: to get past some bad input that scanf is stuck on.

For example, you might have a program that is sitting in a loop reading integers using scanf("%d", &n), and you've discovered that the first time the user types a non-digit character like 'x', the program goes into an infinite loop.

When faced with this situation, I believe you basically have three choices:

  1. Flush the input somehow (if not by using fflush(stdin), then by calling getchar in a loop to read characters until \n, as is often recommended).
  2. Tell the user not to type non-digit characters when digits are expected.
  3. Use something other than scanf to read input.

Now, if you're a beginner, scanf seems like the easiest way to read input, and so choice #3 is scary and difficult. But #2 seems like a real cop-out, because everyone knows that user-unfriendly computer programs are a problem, so it'd be nice to do better. So all too many beginning programmers get painted into a corner, feeling that they have no choice but to do #1. They more or less have to do input using scanf, meaning that it will get stuck on bad input, meaning that they have to figure out a way to flush the bad input, meaning that they're sorely tempted to use fflush(stdin).

I would like to encourage all beginning programmers out there to make a different set of tradeoffs:

  1. During the earliest stages of your C programming career, before you're comfortable using anything other than scanf, just don't worry about bad input. Really. Go ahead and use cop-out #2 above. Think about it like this: You're a beginner, there are lots of things you don't know how to do yet, and one of the things you don't know how to do yet is: deal gracefully with unexpected input.

  2. As soon as you can, learn how to do input using functions other than scanf. At that point, you can start dealing gracefully with bad input, and you'll have many more, much better techniques available to you, that won't require trying to "flush the bad input" at all.

Or, in other words, beginners who are still stuck using scanf should feel free to use cop-out #2, and when they're ready they should graduate from there to technique #3, and nobody should be using technique #1 to try to flush input at all (and certainly not with fflush(stdin).

Steve Summit
  • 29,350
  • 5
  • 43
  • 68
  • One point to nitpick because it is a little ambiguous I think and someone might get you wrong: "*Flush the input somehow (if not by using `fflush(stdin)`, then by calling `getchar` to read characters until `\n`, as is often recommended).*" - One call to `getchar()` doesn´t read character**s** until it finds `\n`. If there are several characters, one call to `getchar()` will only fetch the last character entered, not all up to and also not including the newline. Furthermore, `getchar()` also can consume a newline. – RobertS supports Monica Cellio May 10 '20 at 09:05
  • @RobertSsupportsMonicaCellio Good point, and I don't know why it took so long for me to address it. Wording adjusted to "calling `getchar` in a loop". – Steve Summit Feb 17 '21 at 14:51
3

Using fflush(stdin) to flush input is kind of like dowsing for water using a stick shaped like the letter "S".

And helping people to flush input in some "better" way is kind of like rushing up to an S-stick dowser and saying "No, no, you're doing it wrong, you need to use a Y-shaped stick!".

In other words, the real problem isn't that fflush(stdin) doesn't work. Calling fflush(stdin) is a symptom of an underlying problem. Why are you having to "flush" input at all? That's your problem.

And, usually, that underlying problem is that you're using scanf, in one of its many confusing modes that unexpectedly leaves newlines or other whitespace on the input. The best long-term answer, therefore, is to learn how to do input using better techniques than scanf.

Steve Summit
  • 29,350
  • 5
  • 43
  • 68
  • Though I guess you forgot about this answer https://stackoverflow.com/a/58884121/918959 – Antti Haapala Sep 28 '20 at 21:20
  • @AnttiHaapala Thanks for the pointer, but, no, I didn't forget; both answers are linked in my notes on this topic. There are yet more good, canonical answers at https://stackoverflow.com/questions/34219549/ . – Steve Summit Sep 29 '20 at 10:23
  • What I mean is they're on the same question :D – Antti Haapala Sep 29 '20 at 11:58
  • @AnttiHaapala Yes, I get that. When I posted the second one, SO asked me "You already have an answer to this question, are you sure you want to answer again?", and I answered, "Yes." For these eternal questions, I'm always trying to find different/better/alternative ways of answering them. (Another example is https://stackoverflow.com/questions/949433/ .) – Steve Summit Sep 29 '20 at 12:24
1

Quote from POSIX:

For a stream open for reading, if the file is not already at EOF, and the file is one capable of seeking, the file offset of the underlying open file description shall be set to the file position of the stream, and any characters pushed back onto the stream by ungetc() or ungetwc() that have not subsequently been read from the stream shall be dis- carded (without further changing the file offset).

Note that terminal is not capable of seeking.

Community
  • 1
  • 1
Ryan Chen
  • 132
  • 10