strcmp() 返回 C 语言的值

发布于 2024-12-08 12:01:28 字数 176 浏览 2 评论 0原文

我正在学习 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

高速公鹿 2024-12-15 12:01:28

从这个意义上说,字符串的“小于”意味着字典顺序(字母顺序)。

因此 cat 小于 dog,因为 cat 按字母顺序排列在 dog 之前。

从某种意义上说,字典顺序是字母顺序对所有 ASCII(和 UNICODE)字符的扩展。

In this sense, "less than" for strings means lexicographic (alphabetical) order.

So cat is less than dog because cat is alphabetically before dog.

Lexicographic order is, in some sense, an extension of alphabetical order to all ASCII (and UNICODE) characters.

挖鼻大婶 2024-12-15 12:01:28

大于零的值表示第一个不匹配的字符在第一个字符串中的值大于第二个字符串中的值,小于零的值表示相反。

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.

甜中书 2024-12-15 12:01:28

C99 7.21.4:

比较函数返回的非零值的符号
memcmpstrcmpstrncmp 由符号确定
第一对字符的值之间的差异(两者
解释为 unsigned char),其对象不同
比较。

请特别注意,结果不依赖于当前区域设置; LC_COLLATE(请参阅 C99 7.11)影响 strcoll()strxfrm(),但不影响 strcmp()

C99 7.21.4:

The sign of a nonzero value returned by the comparison functions
memcmp, strcmp, and strncmp is determined by the sign of
the difference between the values of the first pair of characters (both
interpreted as unsigned char) that differ in the objects being
compared.

Note in particular that the result doesn't depend on the current locale; LC_COLLATE (see C99 7.11) affects strcoll() and strxfrm(), but not strcmp().

岁月流歌 2024-12-15 12:01:28
    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;
    }
    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;
    }
作业与我同在 2024-12-15 12:01:28

请注意以下程序,这里我根据您输入的字符串返回值。函数strcmp根据整体考虑的整个字符串的ASCII值返回值。

例如。 str1 = "aab"str2 = "aaa" 将返回 1,因为 aab > ;啊啊。

int main()
{
    char str1[15], str2[15];
    int n;
    printf("Enter the str1 string: ");

    gets(str1);
    printf("Enter the str2 string : ");
    gets(str2);
    n = strcmp(str1, str2);
    printf("Value returned = %d\n", n);
    return 0;
}

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" and str2 = "aaa" will return 1 as aab > aaa.

int main()
{
    char str1[15], str2[15];
    int n;
    printf("Enter the str1 string: ");

    gets(str1);
    printf("Enter the str2 string : ");
    gets(str2);
    n = strcmp(str1, str2);
    printf("Value returned = %d\n", n);
    return 0;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文