Martin Wells wrote On 11/02/07 12:32,:
Let's say we want to write a C89 fully-portable algorithm for making
every character in a string uppercase (where applicable). Is there
anything wrong with the following?
void AllUpper(char *p)
{
while (*p++ = toupper(*p));
}
Yes, if `char' is signed.
Sure, toupper will have unexpected results if the character value is
negative; but, if the implementation has signed char for plain char,
and has negative numbers for some of its characters, isn't it the
implementation's problem to make sure it works properly?
The implementation must work properly when used properly.
When used improperly (as above), all bets are off.
Note that a `char' with the value -1 is likely to be
mistaken for EOF. (EOF is the only legal negative argument
Assuming that the cast to unsigned char is necessary, what will happen
to values which are negative? Will they become corrupt?
Demons will fly from their serifs. Undefined behavior
is "undefined."
In typical implementations where `char' is not too wide,
the <ctype.h> functions use their argument values to index
predefined arrays. Feed such an implementation an out-of-
range argument, and it will try to use that argument as an
array index, with outcomes similar to those you get when you
wander outside your own arrays. You might get a garbage
result like toupper('µ') == '7', or you might get something
like a SIGSEGV or GPF. Some implementations may try to be
helpful by making a spare copy of half the array, but they're
still likely to misbehave with 'ÿ' (Unicode U00FF, easily
confused with EOF on systems with 8-bit signed characters).