一个简单的strcmp函数有关问题,新手分不多,希望各位大侠见谅


一个简单的strcmp函数问题,新手分不多,希望各位大侠见谅
char   line[1024];
cahr   point[1024];            
              char   empty= " ";
              char   *abc= "abc "
              strcpy(line,empty);
              strcpy(point,abc);
              printf( "line=%s\n ",line);
              printf( "point=%s\n ",point);
printf( "strcmp(line,empty)=%d\n ",strcmp(line,empty));          
              printf( "strcmp(line,\ "\ ")=%d\n ",strcmp(line, " "));     \   =0(相等)          
              printf( "strcmp(point,\ "abc\ ")=%d\n ",strcmp(point, "abc "));\=1
              printf( "strcmp(line,\ "abc\ ")=%d\n ",strcmp(point,abc));\=0
********************
打印的结果如下:
line=
point=abc
strcmp(line,empty)=0
strcmp(line, " ")=0
strcmp(point, "abc ")=1
strcmp(point,abc)=0
********************
我有点不明白这个 "abc "和变量abc字符串不是一样的吗?为什么用strcmp比较会出现不同的结果,还有用line与empty和 " "分别比较又是相同的。真迷糊了,C语言中处理字符串是个麻烦的事情啊:)

c语言 函数 程序开发

哎哟我蛋疼 13 years, 4 months ago


终于把这个看懂了~~~
strcmp(point, "abc ")=0
strcmp(line, "abc ")=0
我说这个地方怎么一样呢 原来我看题没有看清楚 ^^ 以后多小心了

微月吨日亿万千 answered 13 years, 4 months ago


strcmp在linux和windows下好象没有什么差别的,他们都是标准的函数。

结果应该是0

貓神八百萬 answered 13 years, 4 months ago


TC2.0,运行结果
line=
point=abc
strcmp(line,empty)=0
strcmp(line, " ")=0
strcmp(point, "abc ")=0
strcmp(line, "abc ")=0

土豆炒马铃薯 answered 13 years, 4 months ago


偶用GCC 結果如下哦
line=
point=abc
strcmp(line,empty)=0
strcmp(line, " ")=0
strcmp(point, "abc ")=0
strcmp(line, "abc ")=0



顶风尿一身 answered 13 years, 4 months ago


//这是类似标准c库的实现,供参考
int strcmp ( unsigned chat * src , unsigned char *dst )
{
int ret = 0 ;

while( ! (ret = *src - *dst) && *dst)
++src, ++dst;

if ( ret < 0 )
ret = -1 ;
else if ( ret > 0 )
ret = 1 ;

return( ret );
}

忽方十四悠 answered 13 years, 4 months ago

Your Answer