strcmp() return values in C [duplicate]
Asked Answered
R

5

22

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?

Remy answered 5/10, 2011 at 3:43 Comment(0)
S
27

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.

Stephaniastephanie answered 5/10, 2011 at 3:48 Comment(2)
From this answer someone could be led to believe that strcmp() knows about unicode. That is not the case.Crusty
@Amigable Clark Kant: For unicode, I was implicitly referring to the wide character version of strcmp(). But yes, I get your point.Stephaniastephanie
N
8

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.

Navigator answered 5/10, 2011 at 3:45 Comment(0)
H
6

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().

Hamman answered 5/10, 2011 at 3:58 Comment(0)
C
2
    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;
    }
Chenault answered 22/11, 2012 at 11:56 Comment(3)
Your answer would be considerably better if you could explain why this answers the question.Falbala
it's also wrong answer, 1/-1 doesn't conform to C standard. And you don't check before dereferencing.Ecbolic
On Ubuntu 14.04 gcc is not returning -1 or 1. It returns (*s1 - *s2)Pagoda
D
-2

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;
}
Decorative answered 21/9, 2013 at 9:43 Comment(1)
Welcome to stackoverflow and thank you for your answer. Please notice that this question was answered over two years ago and has a decent accepted answer. Also, your answer doesn't really answer the original question.Chalcocite

© 2022 - 2024 — McMap. All rights reserved.