我查看了许多strCmp()的实现,发现大多数指针实现都是使用unsigned char。
我的问题是,为什么即使我们不使用"unsigned",返回结果也是一样的(根据我所做的测试)?
如果我不使用它,对于某些值,我会得到错误的结果吗?
最后,char默认是有符号还是无符号的?
示例1
int strCmp(const char* s1, const char* s2)
{
while(*s1 && (*s1 == *s2))
{
s1++;
s2++;
}
return *(const unsigned char*)s1 - *(const unsigned char*)s2;
}
示例2
int strCmp(const char *S1, const char *S2)
{
for(; *S1 == *S2; ++S1, ++S2)
if(*S1 == 0)
return 0;
return *(unsigned char *)S1 < *(unsigned char *)S2 ? -1 : 1;
}