1 .\" Copyright 1993 David Metcalfe (david@prism.demon.co.uk)
2 .\" and Copyright 2020 Michael Kerrisk <mtk.manpages@gmail.com>
4 .\" %%%LICENSE_START(VERBATIM)
5 .\" Permission is granted to make and distribute verbatim copies of this
6 .\" manual provided the copyright notice and this permission notice are
7 .\" preserved on all copies.
9 .\" Permission is granted to copy and distribute modified versions of this
10 .\" manual under the conditions for verbatim copying, provided that the
11 .\" entire resulting derived work is distributed under the terms of a
12 .\" permission notice identical to this one.
14 .\" Since the Linux kernel and libraries are constantly changing, this
15 .\" manual page may be incorrect or out-of-date. The author(s) assume no
16 .\" responsibility for errors or omissions, or for damages resulting from
17 .\" the use of the information contained herein. The author(s) may not
18 .\" have taken the same level of care in the production of this manual,
19 .\" which is licensed free of charge, as they might when working
22 .\" Formatted or processed versions of this manual, if unaccompanied by
23 .\" the source, must acknowledge the copyright and authors of this work.
26 .\" References consulted:
27 .\" Linux libc source code
28 .\" Lewine's _POSIX Programmer's Guide_ (O'Reilly & Associates, 1991)
30 .\" Modified Sat Jul 24 18:08:52 1993 by Rik Faith (faith@cs.unc.edu)
31 .\" Modified 2001-08-31, aeb
33 .TH STRCMP 3 2021-03-22 "" "Linux Programmer's Manual"
35 strcmp, strncmp \- compare two strings
38 .B #include <string.h>
40 .BI "int strcmp(const char *" s1 ", const char *" s2 );
41 .BI "int strncmp(const char *" s1 ", const char *" s2 ", size_t " n );
46 function compares the two strings
50 The locale is not taken into account (for a locale-aware comparison, see
52 The comparison is done using unsigned characters.
55 returns an integer indicating the result of the comparison, as follows:
75 function is similar, except it compares
76 only the first (at most)
87 functions return an integer
88 less than, equal to, or greater than zero if
92 bytes thereof) is found, respectively, to be less than, to
93 match, or be greater than
96 For an explanation of the terms used in this section, see
104 Interface Attribute Value
108 T} Thread safety MT-Safe
114 POSIX.1-2001, POSIX.1-2008, C89, C99, SVr4, 4.3BSD.
116 POSIX.1 specifies only that:
119 The sign of a nonzero return value shall be determined by the sign
120 of the difference between the values of the first pair of bytes
121 (both interpreted as type
122 .IR "unsigned char" )
123 that differ in the strings being compared.
126 In glibc, as in most other implementations,
127 the return value is the arithmetic result of subtracting
128 the last compared byte in
130 from the last compared byte in
132 (If the two characters are equal, this difference is 0.)
134 The program below can be used to demonstrate the operation of
136 (when given two arguments) and
138 (when given three arguments).
139 First, some examples using
144 $ \fB./string_comp ABC ABC\fP
145 <str1> and <str2> are equal
146 $ \fB./string_comp ABC AB\fP # \(aqC\(aq is ASCII 67; \(aqC\(aq \- \(aq\e0\(aq = 67
147 <str1> is greater than <str2> (67)
148 $ \fB./string_comp ABA ABZ\fP # \(aqA\(aq is ASCII 65; \(aqZ\(aq is ASCII 90
149 <str1> is less than <str2> (\-25)
150 $ \fB./string_comp ABJ ABC\fP
151 <str1> is greater than <str2> (7)
152 $ .\fB/string_comp $\(aq\e201\(aq A\fP # 0201 \- 0101 = 0100 (or 64 decimal)
153 <str1> is greater than <str2> (64)
157 The last example uses
158 .BR bash (1)-specific
159 syntax to produce a string containing an 8-bit ASCII code;
160 the result demonstrates that the string comparison uses unsigned
163 And then some examples using
168 $ \fB./string_comp ABC AB 3\fP
169 <str1> is greater than <str2> (67)
170 $ \fB./string_comp ABC AB 2\fP
171 <str1> and <str2> are equal in the first 2 bytes
179 Licensed under GNU General Public License v2 or later.
186 main(int argc, char *argv[])
191 fprintf(stderr, "Usage: %s <str1> <str2> [<len>]\en", argv[0]);
196 res = strcmp(argv[1], argv[2]);
198 res = strncmp(argv[1], argv[2], atoi(argv[3]));
201 printf("<str1> and <str2> are equal");
203 printf(" in the first %d bytes\en", atoi(argv[3]));
205 } else if (res < 0) {
206 printf("<str1> is less than <str2> (%d)\en", res);
208 printf("<str1> is greater than <str2> (%d)\en", res);