简体   繁体   English

C strcmp() 有一个字符差异

[英]C strcmp() with one char difference

My question is that how will strcmp() handle the following case:我的问题是strcmp()将如何处理以下情况:

strcmp("goodpassT", "goodpass");

I read that the comparison continues until a different character is found or null character ( \0 ) is found in any of the strings.我读到比较继续进行,直到找到不同的字符或在任何字符串中找到 null 字符( \0 )。 In the above case, when it encounters \0 for the second argument, will it just stop comparison, or will it still compare to the T character?在上述情况下,当第二个参数遇到\0时,它会停止比较,还是仍然与T字符进行比较? The return value is 1, but I'm not sure about the stopping condition.返回值为 1,但我不确定停止条件。

The comparison is done using unsigned char.比较是使用无符号字符完成的。 Thus the shorter string is smaller as its terminating 0 is smaller than other unsigned nonzero char in the longer string.因此,较短的字符串较小,因为它的终止 0 小于较长字符串中的其他无符号非零字符。 See http://port70.net/~nsz/c/c11/n1570.html#7.24.4p1http://port70.net/~nsz/c/c11/n1570.html#7.24.4p1

The answer for this function strcmp("goodpassT", "goodpass");这个 function strcmp("goodpassT", "goodpass"); 的答案will be 1 only.The point upto which lengths of both the string are same will be compared on the basis of their ASCII value.将仅为 1。两个字符串的长度相同的点将根据它们的 ASCII 值进行比较。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM