2 .\" Copyright 1993 David Metcalfe (david@prism.demon.co.uk)
3 .\" and Copyright 2020 Michael Kerrisk <mtk.manpages@gmail.com>
5 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
7 .\" References consulted:
8 .\" Linux libc source code
9 .\" Lewine's _POSIX Programmer's Guide_ (O'Reilly & Associates, 1991)
11 .\" Modified Sat Jul 24 18:08:52 1993 by Rik Faith (faith@cs.unc.edu)
12 .\" Modified 2001-08-31, aeb
14 .TH strcmp 3 (date) "Linux man-pages (unreleased)"
16 strcmp, strncmp \- compare two strings
19 .RI ( libc ", " \-lc )
22 .B #include <string.h>
24 .BI "int strcmp(const char *" s1 ", const char *" s2 );
25 .BI "int strncmp(const char " s1 [. n "], const char " s2 [. n "], size_t " n );
30 function compares the two strings
34 The locale is not taken into account (for a locale-aware comparison, see
36 The comparison is done using unsigned characters.
39 returns an integer indicating the result of the comparison, as follows:
59 function is similar, except it compares
60 only the first (at most)
71 functions return an integer
72 less than, equal to, or greater than zero if
76 bytes thereof) is found, respectively, to be less than, to
77 match, or be greater than
80 For an explanation of the terms used in this section, see
86 Interface Attribute Value
92 T} Thread safety MT-Safe
95 POSIX.1 specifies only that:
98 The sign of a nonzero return value shall be determined by the sign
99 of the difference between the values of the first pair of bytes
100 (both interpreted as type
101 .IR "unsigned char" )
102 that differ in the strings being compared.
105 In glibc, as in most other implementations,
106 the return value is the arithmetic result of subtracting
107 the last compared byte in
109 from the last compared byte in
111 (If the two characters are equal, this difference is 0.)
115 POSIX.1-2001, C89, SVr4, 4.3BSD.
117 The program below can be used to demonstrate the operation of
119 (when given two arguments) and
121 (when given three arguments).
122 First, some examples using
127 $ \fB./string_comp ABC ABC\fP
128 <str1> and <str2> are equal
129 $ \fB./string_comp ABC AB\fP # \[aq]C\[aq] is ASCII 67; \[aq]C\[aq] \- \[aq]\e0\[aq] = 67
130 <str1> is greater than <str2> (67)
131 $ \fB./string_comp ABA ABZ\fP # \[aq]A\[aq] is ASCII 65; \[aq]Z\[aq] is ASCII 90
132 <str1> is less than <str2> (\-25)
133 $ \fB./string_comp ABJ ABC\fP
134 <str1> is greater than <str2> (7)
135 $ .\fB/string_comp $\[aq]\e201\[aq] A\fP # 0201 \- 0101 = 0100 (or 64 decimal)
136 <str1> is greater than <str2> (64)
140 The last example uses
141 .BR bash (1)-specific
142 syntax to produce a string containing an 8-bit ASCII code;
143 the result demonstrates that the string comparison uses unsigned
146 And then some examples using
151 $ \fB./string_comp ABC AB 3\fP
152 <str1> is greater than <str2> (67)
153 $ \fB./string_comp ABC AB 2\fP
154 <str1> and <str2> are equal in the first 2 bytes
159 .\" SRC BEGIN (string_comp.c)
163 Licensed under GNU General Public License v2 or later.
170 main(int argc, char *argv[])
175 fprintf(stderr, "Usage: %s <str1> <str2> [<len>]\en", argv[0]);
180 res = strcmp(argv[1], argv[2]);
182 res = strncmp(argv[1], argv[2], atoi(argv[3]));
185 printf("<str1> and <str2> are equal");
187 printf(" in the first %d bytes\en", atoi(argv[3]));
189 } else if (res < 0) {
190 printf("<str1> is less than <str2> (%d)\en", res);
192 printf("<str1> is greater than <str2> (%d)\en", res);