gpt4 book ai didi

c - strcmp() 和有符号/无符号字符

转载 作者:太空狗 更新时间:2023-10-29 16:32:02 28 4
gpt4 key购买 nike

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

C 标准定义:

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

The strcmp function compares the string pointed to by s1 to the string pointed to by s2.

The strcmp function returns an integer greater than, equal to, or less than zero, accordingly as the string pointed to by s1 is greater than, equal to, or less than the string pointed to by 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 <stdio.h>
#include <string.h>

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),重点是我的:

The sign of a nonzero value returned by the comparison functions memcmp, strcmp, and strncmp is determined by the sign of the difference between the values of the first pair of characters (both interpreted as unsigned char) that differ in the objects being compared.

C90 中有类似的东西。

请注意,strcoll() 可能比 strcmp() 更适应,尤其是当您的字符超出基本字符集时。

关于c - strcmp() 和有符号/无符号字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1356741/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com