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.

Answer from paxdiablo on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › c language › strcmp-in-c
strcmp() in C %%sep%% %%sitename%% - GeeksforGeeks
December 13, 2025 - It takes two strings (array of characters) as arguments, compares these two strings lexicographically, and then returns some value as a result. The strcmp function in C is used to compare two strings, with syntax: int strcmp(const char *str1, ...
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.

🌐
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.
🌐
TutorialsPoint
tutorialspoint.com › home › c_standard_library › c standard library strcmp function
C Standard Library strcmp Function
August 29, 2012 - Following is the syntax of the C library strcmp() function − ... Zero, if the string are equal. Negative, if the first string is less than second string lexicographically. Positive, if the first string is greater than second string lexicographically. In this example, we demonstrate the usage of string characters comparison using strcmp() function.
🌐
Scaler
scaler.com › home › topics › c strcmp()
C strcmp() - Scaler Topics
April 17, 2024 - The strcmp function, a fundamental ... 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 other....
🌐
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 - A string is an array of characters ... ). Its primary purpose is to compare the characters of the two strings in sequence until it finds a mismatch or until the end of the strings is reached (that is, the null character ...
🌐
BeginnersBook
beginnersbook.com › 2017 › 11 › c-strcmp-function
C strcmp() Function with example
#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; }
Find elsewhere
🌐
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.
🌐
Upgrad
upgrad.com › home › tutorials › software & tech › strcmp in c
strcmp in C | String Comparison Function with Examples
February 5, 2025 - Here's the step-by-step breakdown of how it works: ... strcmp() compares the characters of the two strings at the same position, starting from the first character (index 0).
🌐
Quora
quora.com › How-does-strcmp-work-with-strings
How does strcmp work with strings? - Quora
Answer: For the details of how strcmp() works I would refer you to Calvin Anonar’s answer. I do, however, have some cautions you should consider. Particularly when you are dealing with names. Last names like Del Canto which include a space between the parts of a single last name or those ...
🌐
Wikibooks
en.wikibooks.org › wiki › C_Programming › string.h › strcmp
C Programming/string.h/strcmp - Wikibooks, open books for an open world
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; } However, most real-world implementations will have various optimization tricks to reduce the execution time of the function. One will notice, that strcmp not only returns -1, 0 and +1, but also other negative or positive values, resulting from optimizing away the branching introduced by the ?: operator:
🌐
Cppreference
en.cppreference.com › w › c › string › byte › strcmp
strcmp - cppreference.com
#include <stdio.h> #include <string.h> void demo(const char* lhs, const char* rhs) { const int rc = strcmp(lhs, rhs); const char* rel = rc < 0 ? "precedes" : rc > 0 ?
🌐
SAS
support.sas.com › documentation › onlinedoc › sasc › doc750 › html › lr1 › z2056611.htm
Function Descriptions : strcmp
#include <lcstring.h> #include <stdio.h> #include <stdlib.h> main() { char command[20]; int n = 0; for(;;) { ++n; printf("Enter command # %d\n", n); puts("Enter quit to terminate/any other command to continue"); gets(command_; if (strcmp(command, "quit") == 0) break; /* Determine whether command is equal to quit.
🌐
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 ...
🌐
FreeBSD
forums.freebsd.org › development › userland programming and scripting
C - strcmp in the C standard? | The FreeBSD Forums
August 13, 2022 - FreeBSD’s libc implementation of strcmp() appears to find the first two characters that differ, and return the difference between them. Whereas valgrind’s strcmp() only returns a value from the set {-1,0,1} I don’t have a Linux system to compare as I took my Linux PC to bits.
🌐
IBM
ibm.com › docs › en › i › 7.5.0
strcmp() — Compare Strings
We cannot provide a description for this page right now
🌐
Cplusplus
cplusplus.com › reference › cstring › strcmp
strcmp - cstring
strcmp · function · <cstring> int strcmp ( const char * str1, const char * str2 ); Compare two strings · Compares the C string str1 to the C string str2. This function starts comparing the first character of each string. If they are equal to each other, it continues with the following pairs ...