Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

getchar() used to flush scanf

getchar() used to flush scanf

getchar() used to flush scanf

Consider the following code where the function flush containing getchar is being used to flush scanf when an attempted read from the terminal was unsuccessful.  I was wondering if anyone could shed any light on the behavior discussed below. Thanks and apologies in advance if I have overlooked something very obvious.


#include <stdio.h>
void flush (void) {
            int c;
            while(c=getchar()!= '\n') {
                        printf("Character read %d \n",c);
int main() {
            int success;
            float a;
            while(success==0) {
                        printf("Enter numeric value for a: ");
return 0;

My question deals with the following behavior when values other than the expected floating point value is entered after the prompt during execution.

1.) When you type the word "test" after the prompt during execution you will see the expected output suggesting the four characters in test where found in the buffer before the new line.

2.) When you type the word "eek" at the prompt, you will see only two characters are found (at least using my compiler I see that)

3.) When you type the word "nine" at the prompt, no indication is given that any of the characters in the word nine are found.

It seems that the letters e and n (and their location in the string) are causing the problem.  

RE: getchar() used to flush scanf

VC++ 9 and 10 compilers/RTLs work fine (of course;). No any 'e' or 'n' idiosyncrasies.

By the way: probably you want this loop condition:


while((c=getchar()) != '\n')
Remember operators priority...

RE: getchar() used to flush scanf

getchar on Linux requires a CR before it actually reads anything.  If you wish to read individual characters as they are typed, you need to play with ioctl for the terminal.

You may also be able to use something from the curses library.

On Windows, it should work.  If getchar doesn't work, try _getch() from the conio.h library.

RE: getchar() used to flush scanf

getchar() reads a single char from stdin stream but not from the keyboard. So you must press Enter to pass keyboard line buffer to stdin on Windows (getchar call returns a char after that op only) - it has the same behavior as on Linux.

getch() is the other song (non-standard conio stuff)...

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close