🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c.moderated
Article #2600

Re: fflush(stdin) - not guaranteed to work?

#2600
From: Kazimir Kylheku
Date: Mon, 15 Apr 1996 18:12
64 lines
3054 bytes
In article <4ksjpn$rjt@solutions.solon.com>,
Himanshu Gohel <gohel@csee.usf.edu> wrote:
>In Paul S. Wang's "Introduction to ANSI C on UNIX" he says that
>the fflush() function was "not intended to control input buffering"
>on page 266.
>
>My question is, why?  Some of my students have written programs using
>fflush(stdin) hoping it would clear away anything that's in the input
>buffer before a scanf() statement and apparently it works on some PC
>based compilers, but when re-compiled on a UNIX system, it does not
>always work.

There is a detailed discussion in the comp.lang.c FAQ that covers VMS, UNIX and
DOS in this respect, as far as I recall. It may be downloaded via FTP
from rtfm.mit.edu/put/usenet

Questions about keyboard buffers are very frequent on this newsgroup.

The C language has no concept of a keyboard buffer. The standard IO library
functions have their own buffers. But these are different entities from
hardware keyboard buffers, or buffers internal to the operating system. The
contents of the latter two are inaccessible from the C language.

There may be any number of keystrokes buffered in the OS, or hardware---your
program cannot know this without talking to special drivers or taking other
such similar platform-specific measures that are outside of the scope of C.

>What is the best way to get rid of the '\n' from the input buffer after
>a scanf() statement?  In the same book on page 263 I've seen the following
>format used in an fscanf() format specifier:
>
> %*[\n]
>
>which is explained as follows:
>
>"...and %*[\n] consumes and discards the NEWLINE character at the end of
> an input line."
>
>But I've not had much luck with it when used as follows:
>
>    scanf("%c %*[\n]", &test);

That's because your book is wrong. Which one is it?


You want to match characters that are _not_ equal to a newline:

     scanf("%c %*[^\n]", &test);


The K&R2 book explains this on page 246:

	[...] matches the longest non-empty string of input characters
	      from the set between brackets.

Thus [^\n] means match the longest non-empty string of all input characters
other than a newline.

The newline is then consumed because it is whitespace, and scanf() naturally
thrives on vegetation consisting of spaces, tabs and newlines.

You and your students ought to be using a solid reference like the Kernighan
and Ritchie, or the Harbison and Steele book. Your department can surely afford
a copy of the ISO/IEC standard as well, for reference purposes, no?
Technical Details
Message-ID: <4ku3id$53h@solutions.solon.com>
Path: arc1-ca.pugleaf.net!solon.com!not-for-mail
References: <4ksjpn$rjt@solutions.solon.com>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!solon.com!not-for-mail
From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
Newsgroups: comp.lang.c.moderated,comp.lang.c
Subject: Re: fflush(stdin) - not guaranteed to work?
Date: 15 Apr 1996 13:12:29 -0500
Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
Sender: clc@solutions.solon.com
Approved: clc@solutions.solon.com
Message-ID: <4ku3id$53h@solutions.solon.com>
References: <4ksjpn$rjt@solutions.solon.com>
NNTP-Posting-Host: solutions.solon.com