STRCMP()和符号/无符号的字符 [英] strcmp() and signed / unsigned chars

查看:410
本文介绍了STRCMP()和符号/无符号的字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

C标准定义:


  

INT STRCMP(为const char * S1,为const char * S2);


  
  

该功能的strcmp比较字符串指向S1字符串
  s2指向到。


  
  

该STRCMP函数返回一个整数大于,等于,或
  小于零,因此作为
  指向的字符串S1更大
  比,等于或小于
  字符串s2指向到。


的参数 的char * 。不是的 无符号字符* 。有没有概念,即比较应为进行签名

但是,所有我检查认为高字标准库是这一点,的的值比ASCII-7字符。

我明白这是有用的,预期的行为。我不想说现有的实现是错了或什么。我只是想知道,哪个部分的标准规格有我错过

  INT strcmp_default(为const char * S1,为const char * S2)
{
    而((* S1)及及(* S1 == * S2))
    {
        ++ S1;
        ++ S2;
    }
    返回(* S1 - * S2);
}INT strcmp_unsigned(为const char * S1,为const char * S2)
{
    无符号字符* P1 =(无符号字符*)S1;
    无符号字符* P2 =(无符号字符*)S2;    而((* P1)及及(* P1 == * P2))
    {
        ++ P1;
        ++ P2;
    }
    返回(* P1 - * P2);
}#包括LT&;&stdio.h中GT;
#包括LT&;&string.h中GT;诠释的main()
{
    X1的char [] =ABC;
    X2的char [] =阿布
    的printf(%d个\\ N,strcmp_default(X1,X2));
    的printf(%d个\\ N,strcmp_unsigned(X1,X2));
    的printf(%d个\\ N,STRCMP(X1,X2));
    返回0;
}

输出是:

  103
-153
-153


解决方案

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


  

由比较函数memcmp,STRCMP返回一个非零值的迹象,
  和STRNCMP由第一的值之间的差的符号来确定
  对字符(两者间preTED是unsigned char型),在不同的对象是
  相比较。


有在C90类似的东西。

注意的strcoll()可以更适应比的strcmp(),特别是如果你有基本的字符集以外的字符。

I am confused by strcmp(), or rather, how it is defined by the standard. Consider comparing two strings where one contains characters outside the ASCII-7 range (0-127).

The C standard defines:

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.

The parameters are char *. Not unsigned char *. There is no notion that "comparison should be done as unsigned".

But all the standard libraries I checked consider the "high" character to be just that, higher in value than the ASCII-7 characters.

I understand this is useful and the expected behaviour. I don't want to say the existing implementations are wrong or something. I just want to know, which part in the standard specs have I missed?

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;
}

Output is:

103
-153
-153

解决方案

7.21.4/1 (C99), emphasis is mine:

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.

There is something similar in C90.

Note that strcoll() may be more adapted than strcmp() especially if you have character outside the basic character set.

这篇关于STRCMP()和符号/无符号的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆