]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man3/strcmp.3
err.3: EXAMPLES: use EXIT_FAILURE rather than 1 as exit status
[thirdparty/man-pages.git] / man3 / strcmp.3
1 .\" Copyright 1993 David Metcalfe (david@prism.demon.co.uk)
2 .\" and Copyright 2020 Michael Kerrisk <mtk.manpages@gmail.com>
3 .\"
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.
8 .\"
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.
13 .\"
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
20 .\" professionally.
21 .\"
22 .\" Formatted or processed versions of this manual, if unaccompanied by
23 .\" the source, must acknowledge the copyright and authors of this work.
24 .\" %%%LICENSE_END
25 .\"
26 .\" References consulted:
27 .\" Linux libc source code
28 .\" Lewine's _POSIX Programmer's Guide_ (O'Reilly & Associates, 1991)
29 .\" 386BSD man pages
30 .\" Modified Sat Jul 24 18:08:52 1993 by Rik Faith (faith@cs.unc.edu)
31 .\" Modified 2001-08-31, aeb
32 .\"
33 .TH STRCMP 3 2020-04-11 "" "Linux Programmer's Manual"
34 .SH NAME
35 strcmp, strncmp \- compare two strings
36 .SH SYNOPSIS
37 .nf
38 .B #include <string.h>
39 .PP
40 .BI "int strcmp(const char *" s1 ", const char *" s2 );
41 .PP
42 .BI "int strncmp(const char *" s1 ", const char *" s2 ", size_t " n );
43 .fi
44 .SH DESCRIPTION
45 The
46 .BR strcmp ()
47 function compares the two strings
48 .I s1
49 and
50 .IR s2 .
51 The locale is not taken into account (for a locale-aware comparison, see
52 .BR strcoll (3)).
53 The comparison is done using unsigned characters.
54 .PP
55 .BR strcmp ()
56 returns an integer indicating the result of the comparison, as follows:
57 .IP \(bu 2
58 0, if the
59 .I s1
60 and
61 .I s2
62 are equal;
63 .IP \(bu
64 a negative value if
65 .I s1
66 is less than
67 .IR s2 ;
68 .IP \(bu
69 a positive value if
70 .I s1
71 is greater than
72 .IR s2 ;
73 .PP
74 The
75 .BR strncmp ()
76 function is similar, except it compares
77 only the first (at most)
78 .IR n
79 bytes of
80 .I s1
81 and
82 .IR s2 .
83 .SH RETURN VALUE
84 The
85 .BR strcmp ()
86 and
87 .BR strncmp ()
88 functions return an integer
89 less than, equal to, or greater than zero if
90 .I s1
91 (or the first
92 .I n
93 bytes thereof) is found, respectively, to be less than, to
94 match, or be greater than
95 .IR s2 .
96 .SH ATTRIBUTES
97 For an explanation of the terms used in this section, see
98 .BR attributes (7).
99 .TS
100 allbox;
101 lbw19 lb lb
102 l l l.
103 Interface Attribute Value
104 T{
105 .BR strcmp (),
106 .BR strncmp ()
107 T} Thread safety MT-Safe
108 .TE
109 .SH CONFORMING TO
110 POSIX.1-2001, POSIX.1-2008, C89, C99, SVr4, 4.3BSD.
111 .SH NOTES
112 POSIX.1 specifies only that:
113 .RS
114 .PP
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.
120 .RE
121 .PP
122 In glibc, as in most other implementations,
123 the return value is the arithmetic result of subtracting
124 the last compared byte in
125 .I s2
126 from the last compared byte in
127 .IR s1 .
128 (If the two characters are equal, this difference is 0.)
129 .SH EXAMPLES
130 The program below can be used to demonstrate the operation of
131 .BR strcmp ()
132 (when given two arguments) and
133 .BR strncmp ()
134 (when given three arguments).
135 First, some examples using
136 .BR strcmp ():
137 .PP
138 .in +4n
139 .EX
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)
150 .EE
151 .in
152 .PP
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
157 characters.
158 .PP
159 And then some examples using
160 .BR strncmp ():
161 .PP
162 .in +4n
163 .EX
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
168 .EE
169 .in
170 .SS Program source
171 \&
172 .EX
173 /* string_comp.c
174
175 Licensed under GNU General Public License v2 or later.
176 */
177 #include <stdio.h>
178 #include <stdlib.h>
179 #include <string.h>
180
181 int
182 main(int argc, char *argv[])
183 {
184 int res;
185
186 if (argc < 3) {
187 fprintf(stderr, "Usage: %s <str1> <str2> [<len>]\en", argv[0]);
188 exit(EXIT_FAILURE);
189 }
190
191 if (argc == 3)
192 res = strcmp(argv[1], argv[2]);
193 else
194 res = strncmp(argv[1], argv[2], atoi(argv[3]));
195
196 if (res == 0) {
197 printf("<str1> and <str2> are equal");
198 if (argc > 3)
199 printf(" in the first %d characters\en", atoi(argv[3]));
200 printf("\en");
201 } else if (res < 0) {
202 printf("<str1> is less than <str2> (%d)\en", res);
203 } else {
204 printf("<str1> is greater than <str2> (%d)\en", res);
205 }
206
207 exit(EXIT_SUCCESS);
208 }
209 .EE
210 .SH SEE ALSO
211 .BR bcmp (3),
212 .BR memcmp (3),
213 .BR strcasecmp (3),
214 .BR strcoll (3),
215 .BR string (3),
216 .BR strncasecmp (3),
217 .BR strverscmp (3),
218 .BR wcscmp (3),
219 .BR wcsncmp (3),
220 .BR ascii (7)