fflush()

J

Jack

Why fflush in the following code does not work?

#include <stdio.h>
int main()
{
int n;
char string[80];
for ( n=0 ; n<12 ; n++ )
{
printf( "Enter some words: " );
scanf( "%s", string );
printf( "The first word you entered is : %s\n", string );
fflush ( stdin );
}
return 0;
}

The execution result is as follows:

Enter some words: this is a test.
The first word you entered is : this
Enter some words: The first word you entered is : is
Enter some words: The first word you entered is : a
Enter some words: The first word you entered is : test.
Enter some words:

It seems to me that the stdin is not flushed. Otherwise, the for loop
should stop after 'this' is printed. Am I right?

Thanks.
 
B

Ben Pfaff

Jack said:
fflush ( stdin );

12.26: How can I flush pending input so that a user's typeahead isn't
read at the next prompt? Will fflush(stdin) work?

A: fflush() is defined only for output streams. Since its
definition of "flush" is to complete the writing of buffered
characters (not to discard them), discarding unread input would
not be an analogous meaning for fflush on input streams.

There is no standard way to discard unread characters from a
stdio input stream, nor would such a way necessarily be
sufficient, since unread characters can also accumulate in
other, OS-level input buffers. You may be able to read and
discard characters until \n, or use the curses flushinp()
function, or use some system-specific technique. See also
questions 19.1 and 19.2.

References: ISO Sec. 7.9.5.2; H&S Sec. 15.2.
 
D

Dann Corbit

Jack said:
Why fflush in the following code does not work?

#include <stdio.h>
int main()
{
int n;
char string[80];
for ( n=0 ; n<12 ; n++ )
{
printf( "Enter some words: " );
scanf( "%s", string );
printf( "The first word you entered is : %s\n", string );
fflush ( stdin );
}
return 0;
}

The execution result is as follows:

Enter some words: this is a test.
The first word you entered is : this
Enter some words: The first word you entered is : is
Enter some words: The first word you entered is : a
Enter some words: The first word you entered is : test.
Enter some words:

It seems to me that the stdin is not flushed. Otherwise, the for loop
should stop after 'this' is printed. Am I right?

From the C-FAQ:

12.26: How can I flush pending input so that a user's typeahead isn't
read at the next prompt? Will fflush(stdin) work?

A: fflush() is defined only for output streams. Since its
definition of "flush" is to complete the writing of buffered
characters (not to discard them), discarding unread input would
not be an analogous meaning for fflush on input streams.

There is no standard way to discard unread characters from a
stdio input stream, nor would such a way necessarily be
sufficient, since unread characters can also accumulate in
other, OS-level input buffers. You may be able to read and
discard characters until \n, or use the curses flushinp()
function, or use some system-specific technique. See also
questions 19.1 and 19.2.

References: ISO Sec. 7.9.5.2; H&S Sec. 15.2.
 
R

Rod Pemberton

Jack said:
Why fflush in the following code does not work?

#include <stdio.h>
int main()
{
int n;
char string[80];
for ( n=0 ; n<12 ; n++ )
{
printf( "Enter some words: " );
scanf( "%s", string );
printf( "The first word you entered is : %s\n", string );
fflush ( stdin );
}
return 0;
}

The execution result is as follows:

Enter some words: this is a test.
The first word you entered is : this
Enter some words: The first word you entered is : is
Enter some words: The first word you entered is : a
Enter some words: The first word you entered is : test.
Enter some words:

It seems to me that the stdin is not flushed. Otherwise, the for loop
should stop after 'this' is printed. Am I right?

No. The loop executes twelve times. I see no break statement to terminate
it earlier. Why do you think it should stop after 'this' is printed?

Rod Pemberton
 
K

Kenneth Brody

Rod said:
fflush ( stdin ); [...]
It seems to me that the stdin is not flushed. Otherwise, the for loop
should stop after 'this' is printed. Am I right?

No. The loop executes twelve times. I see no break statement to terminate
it earlier. Why do you think it should stop after 'this' is printed?

Because he erroneously expected that fflush(stdin) would purge any
unread input.

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:[email protected]>
 
K

Keith Thompson

Kenneth Brody said:
Rod said:
fflush ( stdin ); [...]
It seems to me that the stdin is not flushed. Otherwise, the for loop
should stop after 'this' is printed. Am I right?

No. The loop executes twelve times. I see no break statement to terminate
it earlier. Why do you think it should stop after 'this' is printed?

Because he erroneously expected that fflush(stdin) would purge any
unread input.

Even if it did, the loop would execute 12 times, or it would hang
indefinitely, or the program would crash.

Here's the code in question:

| #include <stdio.h>
| int main()
| {
| int n;
| char string[80];
| for ( n=0 ; n<12 ; n++ )
| {
| printf( "Enter some words: " );
| scanf( "%s", string );
| printf( "The first word you entered is : %s\n", string );
| fflush ( stdin );
| }
| return 0;
| }

There are no break statements or other control-flow statements in the
body of the loop. Whatever fflush(stdin) might do, it will not cause
the loop to terminate after fewer than 12 iterations, unless it does
something very strange. (Since it invokes undefined behavior, of
course, that's a real possibility.) If Jack thought the loop would
execute fewer than 12 times, he's misunderstood how for loops work.

(Nothing personal, Jack.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top