strcasecmp用C返回156而不是0,任何想法,为什么? [英] strcasecmp in C returns 156 instead of 0, any ideas why?
问题描述
我有以下的code:
的printf(数数:%d \\ n,strcasecmp(BUF,H \\ n));
和我得到下面的结果,当我尝试在不同的字母堵漏:
A:-7
G:-1
I:1
记者:2
H:156
H:156
应该 strcasecmp
不是当buf是等于 ^ h
或 ^ h 回0 code>?任何想法,为什么它返回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 toH
orh
? 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 withh
orH
?
main(){ char *s = "hH"; printf("%d\n", strcasecmp(s, "Hh")); }
0
read()
also stores whitespace. So, if you are usingread
, 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 10I entered h in the above case.
Also, use
strncasecmp
if you want to compare a fixed number of characters.这篇关于strcasecmp用C返回156而不是0,任何想法,为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!