learning c

Rahul Dhesi dhesi at bsu-cs.UUCP
Tue Apr 4 06:22:00 AEST 1989


In article <584 at greens.UUCP> matthew at sunpix.UUCP ( Sun NCAA) writes:
>c = getchar();
>fflush(stdin);
>
>The variable 'c' will get the first character of input, and the remainder
>of the line (plus the newline) will get flushed away.

Some time ago I noticed that people were assuming that fflush() would
flush pending input data from a file open for reading.  Apparently many
implementations of stdio do this.  I don't think portable code should
rely on it.

The Turbo C 1.0 manual says fflush() on a stream open for reading
flushes the contents of the buffer.  If the stream is line-buffered the
rest of the line will indeed be flushed.

However, the 4.3BSD documentation and my (slightly outdated) System V
Interface Definition both say only that fflush() works on streams
opened for writing.  Streams open for reading are not mentioned in this
context.

     /* portably read one char and ignore rest of line */
     c = getchar();
     while (!feof(stdin) && getchar() != '\n')
	;

I don't know what the ANSI standard says, but for now it probably
doesn't matter.
-- 
Rahul Dhesi         UUCP:  <backbones>!{iuvax,pur-ee}!bsu-cs!dhesi
                    ARPA:  dhesi at bsu-cs.bsu.edu



More information about the Comp.lang.c mailing list