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
- Copyright 1993, David Metcalfe <david@prism.demon.co.uk>
- Copyright 2020, Michael Kerrisk <mtk.manpages@gmail.com>
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms