strncmp(3) Library Functions Manual strncmp(3) NAME strncmp - nonstrings compare LIBRARY Standard C library (libc, -lc) SYNOPSIS #include // see memory.h(3head) int strncmp(const char s1[], const char s2[], size_t n); DESCRIPTION The strncmp() function is similar to strcmp(3), except it compares only the first (at most) n bytes of s1 and s2. It is equivalent to memcmp(s1, s2, MIN(MIN(strnlen(s1,n),strnlen(s2,n))+1, n)) RETURN VALUE See memcmp(3). ATTRIBUTES For an explanation of the terms used in this section, see attributes(7). +--------------------------------------------+---------------+---------+ |Interface | Attribute | Value | +--------------------------------------------+---------------+---------+ |strncmp () | Thread safety | MT-Safe | +--------------------------------------------+---------------+---------+ STANDARDS C11, POSIX.1-2008. HISTORY POSIX.1-2001, C89, SVr4, 4.3BSD. EXAMPLES The program below can be used to demonstrate the operation of strncmp(). $ ./strncmp ABC AB 3; is greater than (67) $ ./strncmp ABC AB 2; and are equal in the first 2 bytes Program source // strncmp.c // Licensed under GNU General Public License v2 or later. #include #include #include int main(int argc, char *argv[]) { int res; if (argc != 4) { fprintf(stderr, "Usage: %s \n", argv[0]); exit(EXIT_FAILURE); } res = strncmp(argv[1], argv[2], atoi(argv[3])); if (res == 0) { printf(" and are equal"); printf(" in the first %d bytes\n", atoi(argv[3])); printf("\n"); } else if (res < 0) { printf(" is less than (%d)\n", res); } else { printf(" is greater than (%d)\n", res); } exit(EXIT_SUCCESS); } SEE ALSO memory.h(3head), memcmp(3), strncasecmp(3), wcsncmp(3) Linux man-pages 6.19 2026-08-10 strncmp(3)