Re: What's wrong ?



santosh wrote:
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) <<< **** CHECK THIS *****

{
long nc;

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

return 0; <<< **** AND THIS *****

}

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?

C99 requires the int in the
declaration of main, and the void for parameters is just good
practice. C99 will supply a default return value, but earlier
standards don't. Thus your code is wrong for any version.

They were omitted for brevity, as you well know.

As I explained above, one of the error fouls C89 thru C95, the
other fouls on C99. So his compiler is accepting erroneous code -
no need to encourage it. Of course I didn't think of the simple
solution of the OP not knowing how to signal EOF. But that is
another subject. The code is still WRONG.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.



--
Posted via a free Usenet account from http://www.teranews.com

.



Relevant Pages

  • Re: Whats wrong ?
    ... Your book is obsolete and wrong. ... C99 will supply a default return value, ... As I explained above, one of the error fouls C89 thru C95, the ... solution of the OP not knowing how to signal EOF. ...
    (comp.lang.c)
  • Re: Whats wrong ?
    ... int main ... while (getchar()!= EOF) ... Your book is obsolete and wrong. ...
    (comp.lang.c)
  • Re: A question regarding Q20.1 from c-faq.com
    ... The standard requires the functions to return EOF on an error ... the standard does not prohibit getcfrom returning EOF ... However, I know for a fact that this issue was discussed on comp.std.c before C99 was approved, and that several actual committee members participated in that discussion. ... As currently written, there's nothing in the standard about EOF exclusively indicating end-of-file or an error, only wording indicating that and end-of-file or error will cause a return value of EOF. ...
    (comp.lang.c)
  • Re: Whats wrong ?
    ... int main ... while (getchar()!= EOF) ... Your book is obsolete and wrong. ...
    (comp.lang.c)
  • Re: Comment on trim string function please
    ... If it is something else you have generated a big foulup. ... the function returns EOF if EOF is passed. ... value for EOF by writing: ... Here's what could work in C99, ...
    (comp.lang.c)