Eric Sosman
5/23/2011 10:23:00 PM
On 5/23/2011 3:38 PM, Keith Thompson wrote:
> jianhua<jhlicc@gmail.com> writes:
>> [...]
>> but this is right:
>>
>> /*
>> Copyright (C) 1991, 1992 Linus Torvalds
>> */
>> int strcmp(const char *cs, const char *ct)
>> {
>> unsigned char c1, c2;
>>
>> while (1) {
>> c1 = *cs++; /*gcc warning: -Wconversion*/
>> c2 = *ct++; /*gcc warning: -Wconversion*/
>> if (c1 != c2)
>> return c1< c2 ? -1 : 1;
>> if (!c1)
>> break;
>> }
>> return 0;
>> }
>
> Yes. More precisely, the first is not portable; it works just fine if
> plain char is unsigned, but it gives incorrect results if plain char is
> signed and some of the values being compared exceed SCHAR_MAX.
I don't think the latter is perfectly portable, either (though
it's portable to all the machines Torvalds was concerned with). On
systems with signed char using ones' complement or signed magnitude
representation, both plain zero and minus zero would convert to zero
as unsigned char (if the latter conversion didn't trap), and would
then be indistinguishable. It's my belief that strcmp() et al.
should treat minus zero as greater than plain zero, because the
former has a 1-bit while the latter does not.
In short, I don't think the Standard's "interpreted as" can be
taken to have the same meaning as "converted to."
--
Eric Sosman
esosman@ieee-dot-org.invalid