Xref: news2.mv.net comp.os.msdos.djgpp:2737 From: Eli Zaretskii Newsgroups: comp.os.msdos.djgpp Subject: Re: End of file keyboard symbol. Date: Sun, 14 Apr 1996 20:29:31 +0200 Organization: NetVision LTD. Lines: 24 Message-ID: References: <316E0DA2 DOT 38EF AT dwnplaza DOT ncom DOT nt DOT gov DOT au> NNTP-Posting-Host: is.elta.co.il Mime-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII In-Reply-To: <316E0DA2.38EF@dwnplaza.ncom.nt.gov.au> To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp On Fri, 12 Apr 1996, Dale Robinson wrote: > while(scanf("%d",&n) != EOF) { > if (n < 0) { > printf("warning: n is -ve\n"); > } else { > printf("%d is correct\n", n); > } Never assume that `scanf' returns EOF when it hits an end of file. You should in general test for the return value to be the expected number of items you want to read (in this case 1), and if the returned value is less than that, take an error action. Btw, the current version of `scanf' has a bug that causes it to NEVER return EOF, but even after that bug is corrected, relying on EOF is unsafe. For instance if you try to read 2 items, like this: scanf ("%d %d", &i, &j); and press ^Z after typing only one number, ANSI C says that `scanf' shouldn't return EOF, but 1. In general, it only has to return EOF when it hits end of file ``before any conversion takes place''.