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
  • πŸ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms