Re: What's wrong ?
- From: CBFalconer <cbfalconer@xxxxxxxxx>
- Date: Thu, 06 Mar 2008 14:56:35 -0500
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
.
- Follow-Ups:
- Re: What's wrong ?
- From: santosh
- Re: What's wrong ?
- References:
- What's wrong ?
- From: Daniel.C
- Re: What's wrong ?
- From: CBFalconer
- Re: What's wrong ?
- From: santosh
- What's wrong ?
- Prev by Date: Re: What's wrong ?
- Next by Date: Re: What's wrong ?
- Previous by thread: Re: What's wrong ?
- Next by thread: Re: What's wrong ?
- Index(es):
Relevant Pages
|