strcmp()和signed / unsigned字符

我对strcmp()感到困惑,或者更确切地说,它是如何由标准定义的。 考虑比较两个字符串,其中一个字符串包含ASCII-7范围之外的字符(0-127)。

C标准定义:

int strcmp(const char * s1,const char * s2);

strcmp函数将s1指向的字符串与s2指向的字符串进行比较。

strcmp函数返回一个大于,等于或小于零的整数,因为s1指向的字符串大于,等于或小于s2指向的字符串。

参数是char * 。 不是unsigned char * 。 没有任何概念“比较应该是unsigned ”。

但是我检查过的所有标准库都认为“高”字符就是那个,它的值高于ASCII-7字符。

我理解这是有用的和预期的行为。 我不想说现有的实现是错误的。 我只是想知道, 我错过了标准规格中的哪一部分

 int strcmp_default( const char * s1, const char * s2 ) { while ( ( *s1 ) && ( *s1 == *s2 ) ) { ++s1; ++s2; } return ( *s1 - *s2 ); } int strcmp_unsigned( const char * s1, const char *s2 ) { unsigned char * p1 = (unsigned char *)s1; unsigned char * p2 = (unsigned char *)s2; while ( ( *p1 ) && ( *p1 == *p2 ) ) { ++p1; ++p2; } return ( *p1 - *p2 ); } #include  #include  int main() { char x1[] = "abc"; char x2[] = "abü"; printf( "%d\n", strcmp_default( x1, x2 ) ); printf( "%d\n", strcmp_unsigned( x1, x2 ) ); printf( "%d\n", strcmp( x1, x2 ) ); return 0; } 

输出是:

 103 -153 -153 

7.21.4 / 1(C99),重点是我的:

比较函数memcmp,strcmp和strncmp返回的非零值的符号由第一对字符( 均被解释为unsigned char )的值之间的差异符号确定,这些符号在被比较的对象中不同。

C90中有类似的东西。

请注意,strcoll()可能比strcmp()更适应,特别是如果您的字符在基本字符集之外。