strncmp(3)
π strncmp(3) π
okay bestie, so this is strncmp from the Library Functions Manual, and honestly? she's a baddie. let's get into it π¦
β¨ NAME β¨
strncmp β nonstrings compare
no bc why does the manpage call it "nonstrings" π iconic of her actually
π LIBRARY π
Standard C library (libc, -lc) π
πΈ SYNOPSIS πΈ
#include <string.h> // see memory.h(3head)
int strncmp(const char s1[], const char s2[], size_t n);
like yes, include the header, yes, here's the signature, and yes, it eats a size_t n. periodt. π
π« DESCRIPTION π«
So strncmp() is basically strcmp(3)'s cousin who knows when to stop talking. She compares only the first (at most) n bytes of s1 and s2. like, n bytes MAX, she's not doing overtime for free.
It is equivalent to:
memcmp(s1, s2, MIN(MIN(strnlen(s1,n),strnlen(s2,n))+1, n))
no cap, that's literally the formula. copy it, don't @ me. π₯
π RETURN VALUE π
See memcmp(3). that's it, that's the section. she's pointing you to her sister's page, iykyk π
π ATTRIBUTES π
For an explanation of the terms used in this section, see attributes(7).
- Interface:
strncmp() - Attribute: Thread safety
- Value:
MT-Safe
(mt-safe means she can hang out in multiple threads at once and there's no drama π)
π STANDARDS π
C11, POSIX.1-2008. certified, validated, official. π«Ά
π HISTORY π
POSIX.1-2001, C89, SVr4, 4.3BSD. she's been around, she's got lore, she's an elder icon. β¨
π§ EXAMPLES π§
okay so this little program below can be used to demonstrate the operation of strncmp(). watch her work:
$ ./strncmp ABC AB 3;
<strn1> is greater than <strn2> (67)
$ ./strncmp ABC AB 2;
<strn1> and <str2> are equal in the first 2 bytes
see how the second one is equal in the first 2 bytes? that's the n doing its thing bestie, she stopped comparing early and called it a tie π
π Program source π
// strncmp.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 != 4) {
fprintf(stderr, "Usage: %s <strn1> <strn2> <n>\n", argv[0]);
exit(EXIT_FAILURE);
}
res = strncmp(argv[1], argv[2], atoi(argv[3]));
if (res == 0) {
printf("<strn1> and <strn2> are equal");
printf(" in the first %d bytes\n", atoi(argv[3]));
printf("\n");
} else if (res < 0) {
printf("<strn1> is less than <strn2> (%d)\n", res);
} else {
printf("<strn1> is greater than <strn2> (%d)\n", res);
}
exit(EXIT_SUCCESS);
}
ate the whole thing, no crumbs. π
π SEE ALSO π
and that's the tea bestie π« go read your other manpages, they miss you. this was Linux man-pages 6.20, dated 2026-08-10, signing off with a little wave ππ mwah!
π¦ 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