'\" t .\" Copyright 1993, David Metcalfe .\" Copyright 2020, Michael Kerrisk .\" Copyright, the authors of the Linux man-pages project .\" .\" SPDX-License-Identifier: Linux-man-pages-copyleft .\" .TH strncmp 3 2026-08-10 "Linux man-pages 6.19" .SH NAME strncmp \- nonstrings compare .SH LIBRARY Standard C library .RI ( libc ,\~ \-lc ) .SH SYNOPSIS .nf .BR #include\~ " // see memory.h(3head)" .P .BR "int strncmp(const char " s1 "[], const char " s2 "[], size_t " n ); .fi .SH DESCRIPTION The .BR strncmp () function is similar to .BR strcmp (3), except it compares only the first (at most) .I n bytes of .I s1 and .IR s2 . .P It is equivalent to .P .in +4n .EX memcmp(s1, s2, MIN(MIN(strnlen(s1,n),strnlen(s2,n))+1, n)) .EE .in .SH RETURN VALUE See .BR memcmp (3). .SH ATTRIBUTES For an explanation of the terms used in this section, see .BR attributes (7). .TS allbox; lbx lb lb l l l. Interface Attribute Value T{ .na .nh .BR strncmp () T} Thread safety MT-Safe .TE .SH STANDARDS C11, POSIX.1-2008. .SH HISTORY POSIX.1-2001, C89, SVr4, 4.3BSD. .SH EXAMPLES The program below can be used to demonstrate the operation of .BR strncmp (). .P .in +4n .EX .RB $ " ./strncmp ABC AB 3" ; is greater than (67) .RB $ " ./strncmp ABC AB 2" ; and are equal in the first 2 bytes .EE .in .SS Program source \& .\" SRC BEGIN (strncmp.c) .EX // 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 \[rs]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\[rs]n", atoi(argv[3])); printf("\[rs]n"); } else if (res < 0) { printf(" is less than (%d)\[rs]n", res); } else { printf(" is greater than (%d)\[rs]n", res); } \& exit(EXIT_SUCCESS); } .EE .\" SRC END .SH SEE ALSO .BR memory.h (3head), .BR memcmp (3), .BR strncasecmp (3), .BR wcsncmp (3)