while((c = getchar()) != '\n' && c != EOF);

This reads input characters until it reaches either the end of the line (i.e., getchar() returned '\n) or end-of-file or an error condition (i.e., getchar() returned EOF).

If stdin is reading from the keyboard, it discards input until you press Enter.

Leaving off the EOF check could give you an infinite loop if there's an input error, or if you trigger an end-of-file condition (on Unix, by typing Ctrl-D twice).

This could be useful, for example, after using scanf() to read an integer. If you execute scanf("%d", &num); and type 123, it will read those 3 digits (and store the value 123 in n), but leave everything after that waiting to be read. The above line can be used to skip the rest of the input line.

(An alternative, likely a better one, is to read whole lines using fgets() and parse them using sscanf().)

This is not equivalent to fflush(stdin). A far as the C standard is concerned, calling fflush on an input stream has undefined behavior.

Some implementations do define the behavior of fflush(stdin). On systems that use GNU libc, for example (most Linux system):

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 the same as discarding input up to the end of the line. And using it makes your code non-portable.

Answer from Keith Thompson on Stack Overflow
Top answer
1 of 2
5
while((c = getchar()) != '\n' && c != EOF);

This reads input characters until it reaches either the end of the line (i.e., getchar() returned '\n) or end-of-file or an error condition (i.e., getchar() returned EOF).

If stdin is reading from the keyboard, it discards input until you press Enter.

Leaving off the EOF check could give you an infinite loop if there's an input error, or if you trigger an end-of-file condition (on Unix, by typing Ctrl-D twice).

This could be useful, for example, after using scanf() to read an integer. If you execute scanf("%d", &num); and type 123, it will read those 3 digits (and store the value 123 in n), but leave everything after that waiting to be read. The above line can be used to skip the rest of the input line.

(An alternative, likely a better one, is to read whole lines using fgets() and parse them using sscanf().)

This is not equivalent to fflush(stdin). A far as the C standard is concerned, calling fflush on an input stream has undefined behavior.

Some implementations do define the behavior of fflush(stdin). On systems that use GNU libc, for example (most Linux system):

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 the same as discarding input up to the end of the line. And using it makes your code non-portable.

2 of 2
1

This code, assuming the file (or some other input) has been opened, will continue to get character by character until it finds a newline (\n) or an end of file (eof).

fflush will just clear the stream for an open file.

Discussions

c++ - Alternative for fflush(stdin)? - Stack Overflow
Is fflush(stdin) is really required in C++ and is it good to do it this way to flush the newline in the buffer? More on stackoverflow.com
๐ŸŒ stackoverflow.com
April 28, 2011
[C] Can someone please explain fflush(stdin) to me?
understand that this command flushes out the input stream No. Calling fflush() on stdin is undefined in C - it could do anything. "flushing" means removing everything from a buffer. In the case of the stdout output stream (for which calling fflush() is defined), then whatever is in the streams internal buffers (private memory) will be sent to the actual output device - if this is the screen, then whatever was in the buffer will be displayed. For alternative ways of clearing the input stream buffer, see http://c-faq.com/stdio/stdinflush2.html . More on reddit.com
๐ŸŒ r/learnprogramming
10
9
July 1, 2015
What's equivalent of fflush( stin ) in C - C++ Forum
I am soon to post an article on this... it is pretty big since the subject is fairly in-depth... but the simple answer is just You can't and/or shouldn't fflush() stdin. Remember, fflush() is not defined on input streams! It does not matter that it always worked for you in the past -- it is ... More on cplusplus.com
๐ŸŒ cplusplus.com
June 28, 2009
c - Replacement of fflush(stdin) - Stack Overflow
Here,it is quitting before giving ... might be having some junk characters.Is there any alternative for flush(stdin).This code snippet is working in Solaris but it is not working in Linux. ... First line of man fflush reads: The function fflush() forces a write of all user-space ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
March 24, 2013
๐ŸŒ
Cprogramming
cboard.cprogramming.com โ€บ c-programming โ€บ 50037-alternative-fflush-stdin.html
alternative for fflush(stdin)
March 1, 2004 - #include <stdio.h> #include <signal.h> void handler(int sig) { printf ("Got signal %d\n", sig); } int main(void) { int i; char buf[BUFSIZ]; for (i = 0; i < NSIG; i++) { signal(i, handler); } fgets(buf, sizeof(buf), stdin); return(0); } I don't believe it is correct to call printf (or most other standard library functions) in a signal handler that is invoked asynchronously.
Top answer
1 of 3
9

The call to fflush(stdin) is undefined behavior in C (and, consequetly, in C++).

The C language standard, ISO 9899:1999 states in 7.19.5.2/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

To discard the entire input up to the next end of line, in C++, the most robust approach is

std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

As for the program "flashing and going away", are you perhaps executing a console application on a Windows platform? Such applications are best executed from a console window (Start->run->cmd.exe)

2 of 3
4

Use cin.get () instead of getchar () and you should be fine. The problem is that C++ I/O streams and C I/O functions are working on top of the same file descriptors, but have different buffers. I can't tell what exactly is going on w/o debugging, but it feels like getchar() that you are calling to pause a program until character is entered is getting characters that were already read by C++ input stream. So it gets data and unblocks, so you exit the program.

If you still have the problem, make sure you don't enter `\n' characters (i.e. don't press enter). Because pressing enter after input is actually a character that you can read. This problem is usually solved with peeking (see http://www.cplusplus.com/reference/iostream/istream/peek/).

๐ŸŒ
Cplusplus
cplusplus.com โ€บ forum โ€บ general โ€บ 12234
What's equivalent of fflush( stin ) in C - C++ Forum
June 28, 2009 - I am soon to post an article on this... it is pretty big since the subject is fairly in-depth... but the simple answer is just You can't and/or shouldn't fflush() stdin. Remember, fflush() is not defined on input streams! It does not matter that it always worked for you in the past -- it is non-standard and will break at some point.
Find elsewhere
๐ŸŒ
Blogger
lekhrajkullu.blogspot.com โ€บ p โ€บ alternate-for-fflushstdin-in-c-for-c.html
Programming Fundamentals: Alternate for fflush(stdin) in c , for c++ . Clearing Input Buffer in c++
The problem comes when a string is inputted after a number. Because the "Enter" pressed after the number still remains in the standard input buffer. In C language the problem can be solved using: fflush(stdin); use it after line used to input number. The above code will clear the enter from input buffer.
๐ŸŒ
Quora
quora.com โ€บ What-is-the-substitute-for-fflush-in-the-C-library
What is the substitute for fflush in the C library? - Quora
But, could not find one. There is no equivalent function call for this function.Even if there are some other functions present, which perform the function of fflush(), they also end up doing somethi...
๐ŸŒ
Sololearn
sololearn.com โ€บ en โ€บ Discuss โ€บ 117448 โ€บ what-is-difference-b-w-fflush-stdin-and-fflush-stdout-in-c-c
What is Difference b/w fflush(stdin) and fflush(stdout) in C/C++? | Sololearn: Learn to code for FREE!
when your program wants to print something it's writes into stdout which is nothing but buffer memory in this case you can change the destination of the stdout to some txt or other format files in which you want to store the output of your program same thing for stdin you can change the path of stdin if you want your program to take input data from some file instead of the terminal
๐ŸŒ
Cplusplus
cplusplus.com โ€บ forum โ€บ general โ€บ 6554
Problem on using fflush(stdin) - C++ Forum
December 23, 2008 - Hi Duoas, I know this is not a good way, but coz i have try to no put the fflush(stdin) but it can't work well, it will only save the name in to address. If u have an alternative way that can help me on this, can you please share with me?
๐ŸŒ
Quora
quora.com โ€บ Why-do-we-use-the-functions-fflush-stdin-and-fflush-stdout-in-c
Why do we use the functions fflush(stdin) and fflush(stdout) in c? - Quora
Answer (1 of 6): Let us first understand the different I/O functions that the standard library provides and their relationship to each other. Output For formatted output, you have fprintf / printf / and their variants. For string output, you have fputs. For output of uninterpreted data i.e. raw ...
๐ŸŒ
C-faq
c-faq.com โ€บ stdio โ€บ stdinflush2.html
Question 12.26b
Alternatively, you can consume the rest of a partially-read line with a simple code fragment like ยท while((c = getchar()) != '\n' && c != EOF) /* discard */ ; (You may also be able to use the curses flushinp function.) There is no standard way to discard unread characters from a stdio input stream. Some vendors do implement fflush so that fflush(stdin...
๐ŸŒ
Quora
quora.com โ€บ What-is-the-difference-between-fflush-stdin-and-fflush-stdout
What is the difference between fflush (stdin) and fflush (stdout)? - Quora
In some environments, fflush(stdin) - which is Undefined behavior - used to โ€œclearโ€ an EOF state so you could continue to read characters from stdin.
๐ŸŒ
Cprogramming
faq.cprogramming.com โ€บ cgi-bin โ€บ smartfaq.cgi
Why fflush(stdin) is wrong - FAQ - Cprogramming.com
How to begin Get the book ยท C tutorial C++ tutorial Game programming Graphics programming Algorithms More tutorials