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 2020-04-11 "" "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 );
42 .BI "int strncmp(const char *" s1 ", const char *" s2 ", size_t " n );
47 function compares the two strings
51 The locale is not taken into account (for a locale-aware comparison, see
53 The comparison is done using unsigned characters.
56 returns an integer indicating the result of the comparison, as follows:
76 function is similar, except it compares
77 only the first (at most)
88 functions return an integer
89 less than, equal to, or greater than zero if
93 bytes thereof) is found, respectively, to be less than, to
94 match, or be greater than
97 For an explanation of the terms used in this section, see
103 Interface Attribute Value
107 T} Thread safety MT-Safe
110 POSIX.1-2001, POSIX.1-2008, C89, C99, SVr4, 4.3BSD.
112 POSIX.1 specifies only that:
115 The sign of a non-zero return value shall be determined by the sign
116 of the difference between the values of the first pair of bytes
117 (both interpreted as type
118 .IR "unsigned char" )
119 that differ in the strings being compared.
122 In glibc, as in most other implementations,
123 the return value is the arithmetic result of subtracting
124 the last compared byte in
126 from the last compared byte in
128 (If the two characters are equal, this difference is 0.)
130 The program below can be used to demonstrate the operation of
132 (when given two arguments) and
134 (when given three arguments).
135 First, some examples using
140 $ \fB./string_comp ABC ABC\fP
141 <str1> and <str2> are equal
142 $ \fB./string_comp ABC AB\fP # \(aqC\(aq is ASCII 67; \(aqC\(aq \- \(aq\0\(aq = 67
143 <str1> is greater than <str2> (67)
144 $ \fB./string_comp ABA ABZ\fP # \(aqA\(aq is ASCII 65; \(aqZ\(aq is ASCII 90
145 <str1> is less than <str2> (\-25)
146 $ \fB./string_comp ABJ ABC\fP
147 <str1> is greater than <str2> (7)
148 $ .\fB/string_comp $\(aq\e201\(aq A\fP # 0201 \- 0101 = 0100 (or 64 decimal)
149 <str1> is greater than <str2> (64)
153 The last example uses
154 .BR bash (1)-specific
155 syntax to produce a string containing an 8-bit ASCII code;
156 the result demonstrates that the string comparison uses unsigned
159 And then some examples using
164 $ \fB./string_comp ABC AB 3\fP
165 <str1> is greater than <str2> (67)
166 $ \fB./string_comp ABC AB 2\fP
167 <str1> and <str2> are equal in the first 2 bytes
175 Licensed under GNU General Public License v2 or later.
182 main(int argc, char *argv[])
187 fprintf(stderr, "Usage: %s <str1> <str2> [<len>]\en", argv[0]);
192 res = strcmp(argv[1], argv[2]);
194 res = strncmp(argv[1], argv[2], atoi(argv[3]));
197 printf("<str1> and <str2> are equal");
199 printf(" in the first %d characters\en", atoi(argv[3]));
201 } else if (res < 0) {
202 printf("<str1> is less than <str2> (%d)\en", res);
204 printf("<str1> is greater than <str2> (%d)\en", res);