Re: What's wrong ?



santosh <santosh.k83@xxxxxxxxx> writes:

CBFalconer wrote:

"Daniel.C" wrote:

I just copied this code from my book with no modification :

#include <stdio.h>
/* count characters in input; 1st version */
main()

int main(void)

{
long nc;

nc = 0;
while (getchar() != EOF)
++nc;
printf("%ld\n", nc);

return 0;

}

There is no output, and no completion either. I tried adding a
"printf" at the end of the code, but it doesn't display.

Your book is obsolete and wrong.

The example probably came from K&R2. So you are saying it's obsolete and
wrong. On what basis?

This should be good....
.



Relevant Pages

  • Re: Whats wrong ?
    ... int main ... while (getchar()!= EOF) ... Your book is obsolete and wrong. ...
    (comp.lang.c)
  • Re: Discarding unread data after scanf()
    ... You need to test for EOF. ... stream, so there's no risk of discarding any more than needed. ... getchar() returns EOF on failure or end of file. ... int skip2nl ...
    (comp.lang.c)
  • Re: Whats the deal with the "toupper" family?
    ... plus EOF which is guaranteed not to be in that range. ... Not according to C89. ... According to C89, getchar() is equivilent ... returning "an unsigned char converted to an int". ...
    (comp.lang.c)
  • Re: C newbie problem
    ... while(c = getchar()!= EOF){ ... Your avoidance of typing has led you to write a program without defined behavior under either the old or new standard. ...
    (comp.lang.c)
  • Re: Why getchar() doesnt quit if EOF isnt the first char
    ... Why getchar() doesn't quit if EOF isn't the first char] ... int main ... It's a value returned by getcharto indicate an end-of-file condition; that value is distinct from any character value. ...
    (comp.lang.c)