Re: A question regarding Q20.1 from c-faq.com
- From: James Kuyper <jameskuyper@xxxxxxxxxxx>
- Date: Wed, 19 Dec 2007 14:30:44 GMT
santosh wrote:
James Kuyper wrote:
Mark McIntyre wrote:jameskuyper@xxxxxxxxxxx wrote:Yes. However, the standard does not prohibit getc() from returning EOFI would disagree; for the small set of routines where EOF isWhy? The standard requires the functions to return EOF on an error
returned through the same channel as valid characters, relying
solely upon an EOF value to diagnose an error/eof condition is the
wrong approach,
condition. It seems to me entirely reasonable for a programmer expect
to be able to rely on a guaranteed behaviour.
when there is no end of file and no error condition. A programmer who
assumes that a value of EOF indicates only those two possibilities is
relying upon a guarantee not actually provided by the standard (though
it is something that is actually true on most real implementations,
explaining the popularity of that assumption).
The intent of the Standard was for an EOF return to indicate end-of-file
or error.
There's considerable reason to believe that C89 was written without considering the possibility of UCHAR_MAX > INT_MAX. 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. Therefore, I believe that this possibility was considered when C99 was written. Yet, neither C99 nor any of the TCs contains any changes that clarify this issue. Therefore, I believe that the standard, as currently written, does indeed reflect the intent of the committee.
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.
7.19.1 (3)
EOF
which expands to an integer constant expression, with type int and a
negative value, that is returned by several functions to indicate
end-of-file, that is, no more input from a stream;
Sure, but does that mean that value of EOF must only mean end-of-file? The very same functions we're talking about (and many others as well) also use EOF to indicate an I/O error, not and end-of-file. The wctob() function is defined as returning EOF for reasons that have nothing to do with I/O.
Taken to it's extreme, your argument would imply that no function is allowed to return a value equal to the value of EOF unless it indicated end-of-file. That would be a rather obscure constraint to apply to, for instance, the atoi() function.
....
....What mandatory requirement does the standard impose which prohibits
getc() from returning EOF when it successfully reads a byte from the
file?
If this is not so, then why have these functions return an int value at
all? They could return an ordinary unsigned char or char value and the
programmer could be told to verify feof and ferror after each and every
read.
EOF would become essentially meaningless on such systems.
C was originally developed on a system where all the char types were 8 bits and int was larger (probably 16 bits, but I don't know for sure). The interface for getc() was chosen when similar systems were the only ones being considered. I doubt very much that C89 was written with the possibility of UCHAR_MAX>INT_MAX in mind. However, it didn't prohibit that possibility, and that possibility can be worked-around, and C99 was approved without any changes addressing that issue, which had been brought up by that time.
.
- References:
- A question regarding Q20.1 from c-faq.com
- From: Logan Lee
- Re: A question regarding Q20.1 from c-faq.com
- From: Mark McIntyre
- Re: A question regarding Q20.1 from c-faq.com
- From: Peter Nilsson
- Re: A question regarding Q20.1 from c-faq.com
- From: santosh
- Re: A question regarding Q20.1 from c-faq.com
- From: santosh
- Re: A question regarding Q20.1 from c-faq.com
- From: Mark McIntyre
- Re: A question regarding Q20.1 from c-faq.com
- From: Mark McIntyre
- Re: A question regarding Q20.1 from c-faq.com
- From: jameskuyper
- Re: A question regarding Q20.1 from c-faq.com
- From: Mark McIntyre
- Re: A question regarding Q20.1 from c-faq.com
- From: James Kuyper
- Re: A question regarding Q20.1 from c-faq.com
- From: santosh
- A question regarding Q20.1 from c-faq.com
- Prev by Date: Re: A question regarding Q20.1 from c-faq.com
- Next by Date: Re: function prototype vs function declaration
- Previous by thread: Re: A question regarding Q20.1 from c-faq.com
- Next by thread: Re: A question regarding Q20.1 from c-faq.com
- Index(es):
Relevant Pages
|