strcmp问题求教

showming 2010-09-17 09:10:10
code:
--------------------------------------------------------------------------------
int __cdecl strcmp (const char *src, const char *dst)
{
int ret = 0 ;
while(!(ret = *(unsigned char *)src - *(unsigned char *)dst) && *dst)这里为什么要做unsigned char *转换?
{
++src;
++dst;
}
if ( ret < 0 )
ret = -1 ;
else if ( ret > 0 )
ret = 1 ;
return( ret );
}
...全文
169 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
mymtom 2010-09-18
  • 打赏
  • 举报
回复
这是strcmp函数的要求。
http://www.opengroup.org/onlinepubs/000095399/functions/strcmp.html

NAME

strcmp - compare two strings

SYNOPSIS

#include <string.h>

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

DESCRIPTION

[CX] [Option Start] The functionality described on this reference page is aligned with the ISO C standard. Any conflict between the requirements described here and the ISO C standard is unintentional. This volume of IEEE Std 1003.1-2001 defers to the ISO C standard. [Option End]

The strcmp() function shall compare the string pointed to by s1 to the string pointed to by s2.

The sign of a non-zero return value shall be determined by the sign of the difference between the values of the first pair of bytes (both interpreted as type unsigned char) that differ in the strings being compared.

RETURN VALUE

Upon completion, strcmp() shall return an integer greater than, equal to, or less than 0, if the string pointed to by s1 is greater than, equal to, or less than the string pointed to by s2, respectively.

ERRORS

No errors are defined.
renzhewh 2010-09-18
  • 打赏
  • 举报
回复
[无符号数的减法运算能保证只要数字每位一样,那么相减必定为0。反之也成立。
而带符号数无法保证这一点。也就是说,当相减为0时,两数的每一位不一定相等。]

这句话,是错的
小魔菇 2010-09-18
  • 打赏
  • 举报
回复
都转化成unsigned 才可以对每个字节的每个bit位进行比较 就不会忽略符号位了
renzhewh 2010-09-18
  • 打赏
  • 举报
回复

补充一点:

这个问题是由unsigned char 与 signed char的取值范围不同造成的
unsigned char 0 -- 255 ,因此 -128被认为是 128
signed char -128 -- 127 ,因此 128 被认为是-128

注:对于二者的交集 0 -- 127 范围内的字符比较,无需转换

上面的[对于数值为-128、0的两个字符来说],顺序反了,应该是0、-128
renzhewh 2010-09-18
  • 打赏
  • 举报
回复

ls 说的不太准确,举的例子也不对;以c=a-b正负作为判断也不准确
有符号类型 a = 0, b = -128, a - b = 128 = -128 , 但是a > b
无符号类型 a = 0, b = -128 = 128, a - b = -128 = 128,但是a < b

strcmp这样做,是为了可移植性,同ls,因为无法确定char 是有符号还是无符号;

对于数值为-128、0的两个字符来说,如果类型为有符号,那么比较的结果为大于,
但是当类型为无符号时,则判断二者小于。

为了避免这种同样的两个字符比较,产生的结果却不同的情况,需要将char 类型
强制转为unsigned char。
mingcsharp 2010-09-18
  • 打赏
  • 举报
回复
类型一样吗?
xxxholic110 2010-09-18
  • 打赏
  • 举报
回复
正如ANSI C标准所规定的:
整数与字符之间的转换到底字符是指无符号字符(unsigned char)还是有符号字符(char)?
当一个字符变量(例如:char c;)转换为对应的等价的无符号整数时使用(unsigned)c,这是错误的。
当这个字符变量转化为无符号整数时,c首先转化为int型整数,这时并不会得到预计的结果。
正确的方式是使用(unsigned char)c.
也为了增强程序的可移植性(具体为什么才用无符号字符查相关文档),采纳了ANSI C标准编译器都是把这里的字符当做无符号字符处理。
以上提的问题也就明白了。
jernymy 2010-09-18
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 mymtom 的回复:]
这是strcmp函数的要求。
http://www.opengroup.org/onlinepubs/000095399/functions/strcmp.html

NAME

strcmp - compare two strings

SYNOPSIS

#include <string.h>

int strcmp(const char *s1, const……
[/Quote]

顶一下
十八道胡同 2010-09-17
  • 打赏
  • 举报
回复
无符号数的减法运算能保证只要数字每位一样,那么相减必定为0。反之也成立。
而带符号数无法保证这一点。也就是说,当相减为0时,两数的每一位不一定相等。

果然是的
某某9 2010-09-17
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 goodboy1881 的回复:]

下面是那个帖子的摘抄。
这是C/C++语言的一个dark corner。
无符号数的减法运算能保证只要数字每位一样,那么相减必定为0。反之也成立。
而带符号数无法保证这一点。也就是说,当相减为0时,两数的每一位不一定相等。

LZ可以试试下面的代码:
C/C++ code
signed char a = 0;
signed char b = -128;
signed char c ……
[/Quote]

支持

无符号数的减法运算能保证只要数字每位一样,那么相减必定为0。反之也成立。
而带符号数无法保证这一点。也就是说,当相减为0时,两数的每一位不一定相等。
积木 2010-09-17
  • 打赏
  • 举报
回复
下面是那个帖子的摘抄。
这是C/C++语言的一个dark corner。
无符号数的减法运算能保证只要数字每位一样,那么相减必定为0。反之也成立。
而带符号数无法保证这一点。也就是说,当相减为0时,两数的每一位不一定相等。

LZ可以试试下面的代码:
signed char a = 0;
signed char b = -128;
signed char c = a - b;


运行程序可知,c为0。但是很显然,a与b并不相等。更要命的是,标准未规定char到底是signed的还是unsigned的。所以,要判断两个变量是否每个bit都相等,必须转换为无符号整数。
积木 2010-09-17
  • 打赏
  • 举报
回复
http://topic.csdn.net/u/20100913/11/094c8e83-1258-473d-a337-3bbb5e7ea77b.html

69,382

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧