strcmp(3)

๐Ÿ’– strcmp(3) โ€” the "are these two strings the same??" girlie ๐Ÿ’–

okay bestie, so this is strcmp, a lil function from the Library Functions Manual (section 3, we love a library function era โœจ). It's literally just comparing strings and we're gonna talk about it for a while, strap in ๐ŸŽ€


๐ŸŒธ NAME ๐ŸŒธ

strcmp โ€” strings compare ๐Ÿ’… (yes that's the actual name, "strings compare", iconic brevity fr fr)


๐Ÿ“š LIBRARY ๐Ÿ“š

Standard C library โ€” aka libc, and you link it with -lc ๐Ÿซถ


โœจ SYNOPSIS โœจ

#include <string.h>

int strcmp(const char *s1, const char *s2);

that's it. two strings walk in, one int walks out ๐Ÿ˜Œ


๐Ÿฆ‹ DESCRIPTION ๐Ÿฆ‹

So strcmp() compares the two strings s1 and s2, and it does it as if by calling memcmp(3). no cap, that's the entire vibe.

It's equivalent to this exactly:

memcmp(s1, s2, MIN(strlen(s1),strlen(s2))+1)

the way that it only compares up to the shorter string's length plus one?? that's the null terminator doing its lil job ๐Ÿงš


๐Ÿ’œ RETURN VALUE ๐Ÿ’œ

Go look at memcmp(3), bestie โ€” it's giving "see also: my sibling explains it" ๐Ÿ™ˆ


๐ŸŽ€ ATTRIBUTES ๐ŸŽ€

For an explanation of the terms used in this section, see attributes(7).

  • Interface: strcmp()
  • Attribute: Thread safety
  • Value: MT-Safe ๐Ÿ”ฅ

(no table for us today, we're doing bullets, we're keeping it cute)


๐Ÿ‘‘ STANDARDS ๐Ÿ‘‘

C11, POSIX.1-2008. certified standard behavior periodt โœจ


๐ŸŒˆ HISTORY ๐ŸŒˆ

POSIX.1-2001, C89, SVr4, 4.3BSD. she's been around, she's a legend, she's literally been doing this since forever ๐Ÿ’€


๐Ÿ˜ญ CAVEATS ๐Ÿ˜ญ

The locale is not taken into account. So if you need a locale-aware comparison, go see strcoll(3) instead, this one is not doing that for you fr fr.

๐Ÿ“ EXAMPLES ๐Ÿ“

The program below can be used to demonstrate the operation of strcmp(). Watch her work:

$ ./strcmp ABC ABC;
<str1> and <str2> are equal
$ ./strcmp ABC AB;      # 'C' is ASCII 67; 'C' - '\0' = 67
<str1> is greater than <str2> (67)
$ ./strcmp ABA ABZ;     # 'A' is ASCII 65; 'Z' is ASCII 90
<str1> is less than <str2> (-25)
$ ./strcmp ABJ ABC;
<str1> is greater than <str2> (7)
$ ./strcmp $'\201' A;   # 0201 - 0101 = 0100 (or 64 decimal)
<str1> is greater than <str2> (64)

okay quick note on that last one: it uses bash(1)-specific syntax to produce a string containing an 8-bit ASCII code, and the result demonstrates that the string comparison uses unsigned characters. not me finding that lowkey fascinating ๐Ÿงšโœจ

๐Ÿ’ป Program source ๐Ÿ’ป

here's the whole source, verbatim, she's adorable:

// strcmp.c
// Licensed under GNU General Public License v2 or later.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int
main(int argc, char *argv[])
{
    int res;

    if (argc != 3) {
        fprintf(stderr, "Usage: %s <str1> <str2>\n", argv[0]);
        exit(EXIT_FAILURE);
    }

    res = strcmp(argv[1], argv[2]);

    if (res == 0) {
        printf("<str1> and <str2> are equal");
        printf("\n");
    } else if (res < 0) {
        printf("<str1> is less than <str2> (%d)\n", res);
    } else {
        printf("<str1> is greater than <str2> (%d)\n", res);
    }

    exit(EXIT_SUCCESS);
}

note the lil printf("<str1> and <str2> are equal"); on its own line and then a separate printf("\n"); โ€” that's just how she is, we respect it ๐Ÿซถ


๐Ÿ‘€ SEE ALSO ๐Ÿ‘€


and that's the whole page bestie!! ๐Ÿ’– this one came from Linux man-pages 6.20, dated 2026-08-10, and honestly? slay. go compare some strings, you got this ๐ŸŒŸ๐Ÿ’…

๐Ÿ“ฆ where this page comes from
  • arch rolling: man-pages 6.20-1 ยท BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
  • nixpkgs unstable: man-pages 6.20
  • ๐Ÿ“œ the page's license: Linux-man-pages-copyleft
  • ๐Ÿ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms