Re: What's wrong ?
- From: santosh <santosh.k83@xxxxxxxxx>
- Date: Sat, 08 Mar 2008 03:06:59 +0530
CBFalconer wrote:
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.
The program isn't in error for C90. It merely returns an unspecified
status value. I don't think that invokes undefined behaviour.
.
- Follow-Ups:
- Re: What's wrong ?
- From: Richard Heathfield
- Re: What's wrong ?
- From: CBFalconer
- Re: What's wrong ?
- References:
- What's wrong ?
- From: Daniel.C
- Re: What's wrong ?
- From: CBFalconer
- Re: What's wrong ?
- From: santosh
- Re: What's wrong ?
- From: CBFalconer
- What's wrong ?
- Prev by Date: Re: Tell me about makefiles
- Next by Date: Re: Tell me about makefiles
- Previous by thread: Re: What's wrong ?
- Next by thread: Re: What's wrong ?
- Index(es):
Relevant Pages
|