C语言 strcmp() 在 C 中究竟返回什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34824838/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 12:17:59  来源:igfitidea点击:

What does strcmp() exactly return in C?

cstringstrcmp

提问by Akhil Raj

I wrote this code 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);
}

Should console print value 1 or difference between ASCIIvalues of oand \0character i.e. 111? On this website, it is written that this should give out put 111, but when I run it on my laptop, it shows 1. Why?

如果控制台打印值1之间的差异ASCIIo\0字符,即111?在这个网站上,写着这应该给出put 111,但是当我在笔记本电脑上运行它时,它显示为1。为什么?

回答by bolov

From the cppreference.com documentation

来自cppreference.com 文档

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.

int strcmp( const char *lhs, const char *rhs );

返回值

  • 如果 lhs 按字典顺序出现在 rhs 之前,则为负值。

  • 如果 lhs 和 rhs 比较相等,则为零。

  • 如果 lhs 按字典顺序出现在 rhs 之后,则为正值。

As you can see it just says negative, zero or positive. You can't count on anything else.

如您所见,它只是表示负数、零或正数。你不能指望别的。

The site you linked isn't incorrect. It tells you that the return value is < 0, == 0or > 0and it gives an example and shows it's output. It doesn't tell the output should be111.

您链接的网站没有错误。它告诉您返回值是< 0, == 0or> 0并给出了一个示例并显示了它的输出。它没有告诉输出应该是111.

回答by Mureinik

To quote the man page:

引用手册页:

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.

如果发现 s1(或其前 n 个字节)分别小于、匹配或大于 s2,则 strcmp() 和 strncmp() 函数返回一个小于、等于或大于零的整数.

In other words, you should never rely on the exact return value of strcmp(other than 0, of course). The only guarantee is that the return value will be negative if the first string is "smaller", positive if the first string is "bigger" or 0if they are equal. The same inputs may generate different results on different platforms with different implementations of strcmp.

换句话说,您永远不应该依赖strcmp0当然,除了)的确切返回值。唯一的保证是,如果第一个字符串“较小”,则返回值为负,如果第一个字符串“较大”或0它们相等,则返回值为正。相同的输入可能会在不同的平台上产生不同的结果,不同的strcmp.

回答by Peter Miehle

strcmpreturns 'a value lesser than 0'if the string1 is alphabetically lesser than string2; zero, if they are equal; and a 'value greater than 0'if string 1 is alphabetically greater than string 2.

strcmp如果 string1 按字母顺序小于 string2,则返回“小于 0 的值”zero,如果它们相等;和“值大于0”,如果串1是按字母顺序不是字符串2大。

回答by Aseem Raj Baranwal

The output depends on the implementation. A decent implementation of the strcmpfunction would be:

输出取决于实现。该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;
}

The above implementation returns -1 if s1 < s2, 1 if s1 > s2 and 0 if s1 = s2.
But usually, there is a faster version which is implemented for actual use:

上面的实现在 s1 < s2 时返回 -1,如果 s1 > s2 时返回 1,如果 s1 = s2 时返回 0。
但通常,有一个更快的版本用于实际使用:

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;
}

Note that this is faster because it skips the branching which was being done previously. Therefore, we usually have the convention that a negative return value means that s1is lexicographically smaller than s2, and a positive value means vice-versa.

请注意,这更快,因为它跳过了之前完成的分支。因此,我们通常有一个约定,负返回值意味着s1字典序小于s2,正值意味着反之。

回答by Claudio Cortese

int strcmp(const char *str1, const char *str2)

will return a value less, equal or greater than 0. If it returns 0 it means that the two strings are equal, if it returns a value minor than 0 it indicates that str1 is less than str2. If it returns a value > 0 it means that str2 is less than str1.

将返回小于、等于或大于 0 的值。如果返回 0 表示两个字符串相等,如果返回小于 0 的值则表示 str1 小于 str2。如果返回值 > 0,则表示 str2 小于 str1。

Your return value is 1 because "Heloooo" is one character more then "Helloo". In fact the word Helloo has 6 characters and Heloooo has 7 characters. Exactly one more char.

您的返回值为 1,因为“Heloooo”比“Helloo”多一个字符。事实上,Helloo 这个词有 6 个字符,而 Heloooo 有 7 个字符。正好多一个字符。

回答by Ashish Ahuja

Basically, strcmp()can return the following:

基本上,strcmp()可以返回以下内容:

  • > 0if the 2nd string is lesser than the first 1st string.

    char s1, s2; strcpy (s1, "helloworld"); strcpy (s2, "world"); int check = strcmp (s1, s2);

  • 0if the strings passed are same.

    char s1, s2; strcpy (s1, "hello"); strcpy (s2, "hello"); int check = strcmp (s1, s2);

    In this case, as strings are equal, check will have 0.

  • < 0if the first string is smaller than the second string.

    char s1, s2; strcpy (s1, "hello"); strcpy (s2, "worldhello"); int check = strcmp (s1, s2);

  • > 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);

So in your case, strcmp ()will return 1as the strings are not equal.

所以在你的情况下,strcmp ()将返回,1因为字符串不相等。

Check out thisand thistutorials about strcmp ().

查看这个这个关于strcmp ().