From 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.

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, == 0 or > 0 and it gives an example and shows it's output. It doesn't tell the output should be 111.

Answer from bolov on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › c language › strcmp-in-c
strcmp() in C %%sep%% %%sitename%% - GeeksforGeeks
December 13, 2025 - The strcmp function in C is used ... strings to compare. It returns 0 if the strings are equal, a negative value if str1 is less than str2, and a positive value if str1 is greater than str2....
🌐
W3Schools
w3schools.com › c › ref_string_strcmp.php
C string strcmp() Function
The strcmp() function compares two strings and returns an integer indicating which one is greater. For this comparison characters at the same position from both strings are compared one by one, starting from the left until one of them does not ...
🌐
Programiz
programiz.com › c-programming › library-function › string.h › strcmp
C strcmp() - C Standard Library
The strcmp() compares two strings character by character. If the strings are equal, the function returns 0. ... The strcmp() function is defined in the string.h header file.
🌐
TechOnTheNet
techonthenet.com › c_language › standard_library_functions › string_h › strcmp.php
C Language: strcmp function (String Compare)
In the C Programming Language, the strcmp function returns a negative, zero, or positive integer depending on whether the object pointed to by s1 is less than, equal to, or greater than the object pointed to by s2.
🌐
The Open Group
pubs.opengroup.org › onlinepubs › 009604499 › functions › strcmp.html
strcmp
The strcmp() function shall compare the string pointed to by s1 to the string pointed to by s2. The sign of a non-zero return value shall be determined by the sign of the difference between the values of the first pair of bytes (both interpreted as type unsigned char) that differ in the strings ...
🌐
TutorialsPoint
tutorialspoint.com › home › c_standard_library › c standard library strcmp function
C Standard Library strcmp Function
August 29, 2012 - In this code, str1 ("text") comes before str2 ("Notebook") lexicographically. The strcmp() function returns a negative value.
Find elsewhere
Top answer
1 of 9
36

The pseudo-code "implementation" of strcmp would go something like:

define strcmp (str1, str2):
    p1 = address of first character of str1
    p2 = address of first character of str2

    while p1 not at end of str1:
        if p2 at end of str2: 
            return 1

        if contents of p2 greater than contents of p1:
            return -1

        if contents of p1 greater than contents of p2:
            return 1

        advance p1
        advance p2

    if p2 not at end of str2:
        return -1

    return 0

That's basically it. Each character is compared in turn and a decision is made as to whether the first or second string is greater(a), based on that character.

Only if the characters are identical do you move to the next character and, if all the characters were identical, zero is returned.

Note that you may not necessarily get 1 and -1, the specs say that any positive or negative value will suffice, so you should always check the return value with < 0, > 0 or == 0.

Turning that into real C would result in something like this:

int myStrCmp (const char *str1, const char *str2) {
    const unsigned char *p1 = (const unsigned char *) str1;
    const unsigned char *p2 = (const unsigned char *) str2;

    while (*p1 != '\0') {
        if (*p2 == '\0') return  1;
        if (*p2 > *p1)   return -1;
        if (*p1 > *p2)   return  1;

        p1++;
        p2++;
    }

    if (*p2 != '\0') return -1;

    return 0;
}

(a) Keep in mind that "greater" in the context of characters is not necessarily based on simple ASCII ordering for all string functions.

C has a concept called 'locales' which specify (amongst other things) the collation (ordering of the underlying character set).

You may therefore find, for example, that the characters from the set {a, á, à, ä} are all considered identical when comparing.

2 of 9
10

Here is the BSD implementation:

int
strcmp(s1, s2)
    register const char *s1, *s2;
{
    while (*s1 == *s2++)
        if (*s1++ == 0)
            return (0);
    return (*(const unsigned char *)s1 - *(const unsigned char *)(s2 - 1));
}

Once there is a mismatch between two characters, it just returns the difference between those two characters.

🌐
SAS
support.sas.com › documentation › onlinedoc › sasc › doc750 › html › lr1 › z2056611.htm
Function Descriptions : strcmp
The return value from strcmp is 0 if the two strings are equal, less than 0 if str1 compares less than str2 , and greater than 0 if str1 compares greater than str2 . No other assumptions should be made about the value returned by strcmp . If one of the arguments of strcmp is not properly ...
🌐
IBM
ibm.com › docs › en › i › 7.5.0
strcmp() — Compare Strings
We cannot provide a description for this page right now
🌐
Scaler
scaler.com › home › topics › c strcmp()
C strcmp() - Scaler Topics
April 17, 2024 - The strcmp function, a fundamental ... lexicographically. strcmp in C takes two character arrays as parameters and returns an integer value, which signifies whether the strings are equal, or if one is greater or less than the ...
🌐
BeginnersBook
beginnersbook.com › 2017 › 11 › c-strcmp-function
C strcmp() Function with example
However this function doesn’t compare length, it matches the ASCII value of each character of first string with the second string and returns positive if the ASCII value of first unmatched character in first string is greater than the ASCII value of the unmatched character of second string · Lets take an example to understand this. #include <stdio.h> #include <string.h> int main () { char str1[20]; char str2[20]; int result; //Assigning the value to the string str1 strcpy(str1, "hello"); //Assigning the value to the string str2 strcpy(str2, "hEllo"); result = strcmp(str1, str2); if(result > 0) { printf("ASCII value of first unmatched character of str1 is greater than str2"); } else if(result < 0) { printf("ASCII value of first unmatched character of str1 is less than str2"); } else { printf("Both the strings str1 and str2 are equal"); } return 0; }
🌐
freeCodeCamp
freecodecamp.org › news › strcmp-in-c-how-to-compare-strings-in-c
strcmp in C – How to Compare Strings in C
April 27, 2023 - The strcmp() function compares ... banana. Therefore, the function strcmp() returns a negative value indicating that the first string is less than the second string....
🌐
FreeBSD
forums.freebsd.org › development › userland programming and scripting
C - strcmp in the C standard? | The FreeBSD Forums
August 13, 2022 - The value returned is obtained by subtracting the characters at the first position where s and t disagree" So, I'm going to take K&R as authoritative and strcmp should be returning the difference of the characters, not "-1, 0, +1".
🌐
Upgrad
upgrad.com › home › tutorials › software & tech › strcmp in c
strcmp in C | String Comparison Function with Examples
February 5, 2025 - The student assumes full responsibility for all expenses associated with visas, travel, & related costs. upGrad does not . Read More ... Because the strcmp function checks every character—case and all. The strcmp in C function is used to compare two strings character by character. It returns 0 if both strings are equal, a positive value if the first string is greater, and a negative value if it’s smaller.
🌐
Cppreference
en.cppreference.com › w › c › string › byte › strcmp
strcmp - cppreference.com
"follows" : "equals"; printf("[%s] %s [%s]\n", lhs, rel, rhs); } int main(void) { const char* string = "Hello World!"; demo(string, "Hello!"); demo(string, "Hello"); demo(string, "Hello there"); demo("Hello, everybody!" + 12, "Hello, somebody!" + 11); } Output: [Hello World!] precedes [Hello!] [Hello World!] follows [Hello] [Hello World!] precedes [Hello there] [body!] equals [body!] C23 standard (ISO/IEC 9899:2024): 7.24.4.2 The strcmp function (p: TBD) C17 standard (ISO/IEC 9899:2018): 7.24.4.2 The strcmp function (p: TBD) C11 standard (ISO/IEC 9899:2011): 7.24.4.2 The strcmp function (p: 365-366) C99 standard (ISO/IEC 9899:1999): 7.21.4.2 The strcmp function (p: 328-329) C89/C90 standard (ISO/IEC 9899:1990): 4.11.4.2 The strcmp function ·
🌐
Weber State University
icarus.cs.weber.edu › ~dab › cs1410 › textbook › 8.Strings › strcmp.html
8.2.2.4. strcmp
When comparing instances of the string class, the relational operators evaluate to a boolean value: true or false. The strcmp function is the only valid way to compare C-strings, and it returns a negative value, 0, or a positive value. The following figure explores the meaning of the returned ...
🌐
Cplusplus
cplusplus.com › reference › cstring › strcmp
strcmp - cstring
C string to be compared. Returns an integral value indicating the relationship between the strings:
🌐
Cprogramming
cboard.cprogramming.com › c-programming › 134160-strcmp-return-value.html
strcmp return value
Synopsis #include <string.h> int strcmp(const char *s1, const char *s2); 2. Description The strcmp function compares the string pointed to by s1 to the string pointed to by s2. 3. Returns The strcmp function returns an integer greater than, equal to, or less than zero, accordingly as the string ...