strcmp() 返回 C 语言的值
我正在学习 C 语言中的 strcmp()
。我知道当两个字符串相等时,strcmp
返回 0。
但是,当手册页指出 strcmp<当第一个字符串小于第二个字符串时 /code> 返回小于 0,它是指长度、ASCII 值还是其他什么?
I am learning about strcmp()
in C. I understand that when two strings are equal, strcmp
returns 0.
However, when the man pages state that strcmp
returns less than 0 when the first string is less than the second string, is it referring to length, ASCII values, or something else?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
从这个意义上说,字符串的“小于”意味着字典顺序(字母顺序)。
因此
cat
小于dog
,因为cat
按字母顺序排列在dog
之前。从某种意义上说,字典顺序是字母顺序对所有 ASCII(和 UNICODE)字符的扩展。
In this sense, "less than" for strings means lexicographic (alphabetical) order.
So
cat
is less thandog
becausecat
is alphabetically beforedog
.Lexicographic order is, in some sense, an extension of alphabetical order to all ASCII (and UNICODE) characters.
大于零的值表示第一个不匹配的字符在第一个字符串中的值大于第二个字符串中的值,小于零的值表示相反。
A value greater than zero indicates that the first character that does not match has a greater value in the first string than in the second, and a value less than zero indicates the opposite.
C99 7.21.4:
请特别注意,结果不依赖于当前区域设置;
LC_COLLATE
(请参阅 C99 7.11)影响strcoll()
和strxfrm()
,但不影响strcmp()
。C99 7.21.4:
Note in particular that the result doesn't depend on the current locale;
LC_COLLATE
(see C99 7.11) affectsstrcoll()
andstrxfrm()
, but notstrcmp()
.请注意以下程序,这里我根据您输入的字符串返回值。函数
strcmp
根据整体考虑的整个字符串的ASCII值返回值。例如。
str1 = "aab"
和str2 = "aaa"
将返回 1,因为 aab > ;啊啊。Look out the following program, here I am returning the value depending upon the string you have typed. The function
strcmp
retrun value according to ASCII value of whole string considered totally.For eg.
str1 = "aab"
andstr2 = "aaa"
will return 1 as aab > aaa.