Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!iuvax!bsu-cs!dhesi From: dhesi@bsu-cs.UUCP (Rahul Dhesi) Newsgroups: comp.lang.c Subject: Re: learning c Summary: Keep portability in mind Keywords: getchar() Message-ID: <6487@bsu-cs.UUCP> Date: 3 Apr 89 20:22:00 GMT References: <1091@bnlux0.bnl.gov> <584@greens.UUCP> Reply-To: dhesi@bsu-cs.UUCP (Rahul Dhesi) Distribution: usa Organization: CS Dept, Ball St U, Muncie, Indiana Lines: 31 In article <584@greens.UUCP> matthew@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: !{iuvax,pur-ee}!bsu-cs!dhesi ARPA: dhesi@bsu-cs.bsu.edu