John E. Hadstate wrote:
> "Bryan Olson" wrote:
>>I was therefore quite surprised
>>when "infobahn" pointed out that the standard [ISO/IEC
>>9899:1999] reads:
>>
>> 7.4 (ctype.h)
>>
>> The header <ctype.h> declares several functions useful for
>> classifying and mapping characters.166) In all cases the
>> argument is an int, the value of which shall be
>> representable as an unsigned char or shall equal the value
>> of the macro EOF. If the argument has any other value, the
>> behavior is undefined.
>
> That seems pretty clear to me. It seems to me that your
> beef is with the way some particular implementation defines
> behavior that the C standard doesn't.
I wasn't considering particular implementations. My problem was
that Harbison and Steele's highly respected /C, A Reference
Manual/ disagreed with the standard, in saying these functions
are *also* defined for all values representable as type char:
All of the facilities described here operate properly on all
values representable as type char or type unsigned char, and
also for the value EOF
In an implementation where char is signed, is toupper() defined
for the negative char values (other than EOF)? H&S says yes, the
standard says no. I don't think that's what the standard
*should* say, but it does and it's the standard, so H&S is
wrong. Similarly, Gwyn was wrong when he wrote:
The toupper function has an int argument, not char, and it
is perfectly safe to feed it any character code (or EOF).
According to the standard, that is false. Character codes may be
negative, and toupper() yields undefined behavior for any such
code that doesn't happen to equal EOF.
> The C standard is riddled with "undefined behavior". One
> can argue until the cows come home (been there, done that)
> that a standard that canonizes "undefined behavior" is not a
> standard at all. It's this issue, more than anything else,
> that makes C a dangerous and unpredictable programming
> environment where even highly skilled and experienced
> programmers occasionally get bit.
Absolutely.
And I think the cows came and left several times over during
that "[Lit] Buffer overruns" thread a few months ago.
--
--Bryan
Received on Thu Sep 29 21:38:28 2005