strcasecmp用C返回156而不是0,任何想法,为什么? [英] strcasecmp in C returns 156 instead of 0, any ideas why?

查看:181
本文介绍了strcasecmp用C返回156而不是0,任何想法,为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的code:

 的printf(数数:%d \\ n,strcasecmp(BUF,H \\ n));

和我得到下面的结果,当我尝试在不同的字母堵漏:

  A:-7
G:-1
I:1
记者:2
H:156
H:156

应该 strcasecmp 不是当buf是等于 ^ h ^ h ?任何想法,为什么它返回156?我需要弄清楚如何检查是否用户键入<大骨节病> ^ h 或<大骨节病> ^ h

谢谢!

编辑:我在读buf中通过以下方式:
阅读(0,BUF,MAXBUFLEN);


解决方案

 的printf(数数:%d \\ n,strcasecmp(BUF,H));

为什么 \\ n 在最后,如果你想与比较 ^ h ^ h


 的main(){
 字符* S =HH;
 的printf(%d个\\ N,strcasecmp(S,HH));
}


  



阅读()也存储空白。所以,如果你正在使用,比较H \\ N。

 的main(){
 字符* BUF =的malloc(sizeof的(字符)* 10);
 读(0,BUF,10);
 的printf(%S%D \\ n,BUF,strcasecmp(BUF,H \\ N),strcasecmp(BUF,H));
}


  

^ h结果
  ^ h结果
  &NBSP; 0 10


我进入<大骨节病> ^ h 在上述情况下。

此外,使用 strncasecmp 如果要比较的字符固定数量的。

I have the following code:

printf("num: %d\n", strcasecmp(buf, "h\n"));

And I get the following results when I try plugging in different letters:

a: -7
g: -1
i: 1
j: 2
h: 156
H: 156

Should strcasecmp not return 0 when buf is equal to H or h? Any ideas why it's returning 156? I need to figure out how to check whether the user types H or h.

Thanks!

Edit: I'm reading buf in the following way: read(0, buf, MAXBUFLEN);

解决方案

printf("num: %d\n", strcasecmp(buf, "h"));

Why \n at the end, if you want to compare with h or H ?


main(){
 char *s = "hH";
 printf("%d\n", strcasecmp(s, "Hh"));
}

0


read() also stores whitespace. So, if you are using read, compare with "h\n".

main(){
 char *buf = malloc(sizeof(char)*10);
 read(0, buf, 10);
 printf("%s %d %d\n", buf, strcasecmp(buf, "h\n"), strcasecmp(buf, "h"));
}

h
h
  0 10

I entered h in the above case.

Also, use strncasecmp if you want to compare a fixed number of characters.

这篇关于strcasecmp用C返回156而不是0,任何想法,为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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