C 中的 strcmp() 到底是什么 return ?
What does strcmp() exactly return in C?
我用 C:
写了这段代码
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
int main()
{
char string1[20];
char string2[20];
strcpy(string1, "Heloooo");
strcpy(string2, "Helloo");
printf("%d", strcmp(string1, string2));
return(0);
}
控制台应该打印值 1 还是 o
和 [=14=]
字符的 ASCII
值之间的差异,即 111?在 this website 上写着这应该给出 put 111,但是当我 运行 它在我的笔记本电脑上时,它显示 1。为什么?
strcmp
returns 'a value lesser than 0' 如果 string1 按字母顺序小于 string2; zero
,如果相等;和 'value greater than 0' 如果字符串 1 按字母顺序大于字符串 2.
来自the cppreference.com documentation
int strcmp( const char *lhs, const char *rhs );
Return value
Negative value if lhs appears before rhs in lexicographical order.
Zero if lhs and rhs compare equal.
Positive value if lhs appears after rhs in lexicographical order.
如您所见,它只表示负数、零或正数。你不能指望其他任何东西。
您链接的网站没有错误。它告诉您 return 值为 < 0
、== 0
或 > 0
,并给出了示例并显示了它的输出。它没有告诉输出 应该是 111
.
引用手册页:
The strcmp() and strncmp() functions return an integer less than, equal to, or greater than zero if s1 (or the first n bytes thereof) is found, respectively, to be less than, to match, or be greater than s2.
换句话说,您永远不应依赖 strcmp
的确切 return 值(当然 0
除外)。唯一的保证是如果第一个字符串是 "smaller",return 值将为负,如果第一个字符串是 "bigger" 或 0
,则为正,如果它们相等。相同的输入可能会在不同平台上使用不同的 strcmp
实现产生不同的结果。
输出取决于实现。 strcmp
函数的一个体面的实现是:
int strcmp (const char * s1, const char * s2)
{
for(; *s1 == *s2; ++s1, ++s2)
if(*s1 == 0)
return 0;
return *(unsigned char *)s1 < *(unsigned char *)s2 ? -1 : 1;
}
以上实现 returns -1 if s1 < s2, 1 if s1 > s2 and 0 if s1 = s2.
但通常,有一个更快的版本是为实际使用而实现的:
int strcmp (const char * s1, const char * s2)
{
for(; *s1 == *s2; ++s1, ++s2)
if(*s1 == 0)
return 0;
return *(const unsigned char *)s1 - *(const unsigned char *)s2;
}
请注意,这样做速度更快,因为它跳过了之前进行的分支。因此,我们通常有一个约定,负值 return 表示 s1
在字典序上小于 s2
,正值表示 vice-versa.
int strcmp(const char *str1, const char *str2)
将 return 小于、等于或大于 0 的值。如果它 return 是 0 则意味着两个字符串相等,如果它 return 是小于的值0 表示 str1 小于 str2。如果它 return 的值 > 0,则表示 str2 小于 str1。
您的 return 值为 1,因为 "Heloooo" 比 "Helloo" 多一个字符。
事实上,Hellooo 这个词有 6 个字符,而 Heloooo 有 7 个字符。正好多了一个字符。
基本上,strcmp()
可以 return 以下内容:
> 0
如果第二个字符串小于第一个字符串。
char s1, s2;
strcpy (s1, "helloworld");
strcpy (s2, "world");
int check = strcmp (s1, s2);
0
如果传递的字符串相同。
char s1, s2;
strcpy (s1, "hello");
strcpy (s2, "hello");
int check = strcmp (s1, s2);
在这种情况下,由于字符串相等,检查将有 0
.
< 0
如果第一个字符串小于第二个字符串。
char s1, s2;
strcpy (s1, "hello");
strcpy (s2, "worldhello");
int check = strcmp (s1, s2);
所以在你的情况下,strcmp ()
将 return 1
因为字符串不相等。
查看关于 strcmp ()
.
的 this 和 this 教程
我用 C:
写了这段代码#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
int main()
{
char string1[20];
char string2[20];
strcpy(string1, "Heloooo");
strcpy(string2, "Helloo");
printf("%d", strcmp(string1, string2));
return(0);
}
控制台应该打印值 1 还是 o
和 [=14=]
字符的 ASCII
值之间的差异,即 111?在 this website 上写着这应该给出 put 111,但是当我 运行 它在我的笔记本电脑上时,它显示 1。为什么?
strcmp
returns 'a value lesser than 0' 如果 string1 按字母顺序小于 string2; zero
,如果相等;和 'value greater than 0' 如果字符串 1 按字母顺序大于字符串 2.
来自the cppreference.com documentation
int strcmp( const char *lhs, const char *rhs );
Return value
Negative value if lhs appears before rhs in lexicographical order.
Zero if lhs and rhs compare equal.
Positive value if lhs appears after rhs in lexicographical order.
如您所见,它只表示负数、零或正数。你不能指望其他任何东西。
您链接的网站没有错误。它告诉您 return 值为 < 0
、== 0
或 > 0
,并给出了示例并显示了它的输出。它没有告诉输出 应该是 111
.
引用手册页:
The strcmp() and strncmp() functions return an integer less than, equal to, or greater than zero if s1 (or the first n bytes thereof) is found, respectively, to be less than, to match, or be greater than s2.
换句话说,您永远不应依赖 strcmp
的确切 return 值(当然 0
除外)。唯一的保证是如果第一个字符串是 "smaller",return 值将为负,如果第一个字符串是 "bigger" 或 0
,则为正,如果它们相等。相同的输入可能会在不同平台上使用不同的 strcmp
实现产生不同的结果。
输出取决于实现。 strcmp
函数的一个体面的实现是:
int strcmp (const char * s1, const char * s2)
{
for(; *s1 == *s2; ++s1, ++s2)
if(*s1 == 0)
return 0;
return *(unsigned char *)s1 < *(unsigned char *)s2 ? -1 : 1;
}
以上实现 returns -1 if s1 < s2, 1 if s1 > s2 and 0 if s1 = s2.
但通常,有一个更快的版本是为实际使用而实现的:
int strcmp (const char * s1, const char * s2)
{
for(; *s1 == *s2; ++s1, ++s2)
if(*s1 == 0)
return 0;
return *(const unsigned char *)s1 - *(const unsigned char *)s2;
}
请注意,这样做速度更快,因为它跳过了之前进行的分支。因此,我们通常有一个约定,负值 return 表示 s1
在字典序上小于 s2
,正值表示 vice-versa.
int strcmp(const char *str1, const char *str2)
将 return 小于、等于或大于 0 的值。如果它 return 是 0 则意味着两个字符串相等,如果它 return 是小于的值0 表示 str1 小于 str2。如果它 return 的值 > 0,则表示 str2 小于 str1。
您的 return 值为 1,因为 "Heloooo" 比 "Helloo" 多一个字符。 事实上,Hellooo 这个词有 6 个字符,而 Heloooo 有 7 个字符。正好多了一个字符。
基本上,strcmp()
可以 return 以下内容:
> 0
如果第二个字符串小于第一个字符串。char s1, s2; strcpy (s1, "helloworld"); strcpy (s2, "world"); int check = strcmp (s1, s2);
0
如果传递的字符串相同。char s1, s2; strcpy (s1, "hello"); strcpy (s2, "hello"); int check = strcmp (s1, s2);
在这种情况下,由于字符串相等,检查将有
0
.< 0
如果第一个字符串小于第二个字符串。char s1, s2; strcpy (s1, "hello"); strcpy (s2, "worldhello"); int check = strcmp (s1, s2);
所以在你的情况下,strcmp ()
将 return 1
因为字符串不相等。
查看关于 strcmp ()
.