summaryrefslogtreecommitdiffstats
path: root/man3/strcmp.3
blob: 5466a8217fb01ab34a23cbf5526af476727f7be0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
.\" Copyright 1993 David Metcalfe (david@prism.demon.co.uk)
.\" and Copyright 2020 Michael Kerrisk <mtk.manpages@gmail.com>
.\"
.\" %%%LICENSE_START(VERBATIM)
.\" Permission is granted to make and distribute verbatim copies of this
.\" manual provided the copyright notice and this permission notice are
.\" preserved on all copies.
.\"
.\" Permission is granted to copy and distribute modified versions of this
.\" manual under the conditions for verbatim copying, provided that the
.\" entire resulting derived work is distributed under the terms of a
.\" permission notice identical to this one.
.\"
.\" Since the Linux kernel and libraries are constantly changing, this
.\" manual page may be incorrect or out-of-date.  The author(s) assume no
.\" responsibility for errors or omissions, or for damages resulting from
.\" the use of the information contained herein.  The author(s) may not
.\" have taken the same level of care in the production of this manual,
.\" which is licensed free of charge, as they might when working
.\" professionally.
.\"
.\" Formatted or processed versions of this manual, if unaccompanied by
.\" the source, must acknowledge the copyright and authors of this work.
.\" %%%LICENSE_END
.\"
.\" References consulted:
.\"     Linux libc source code
.\"     Lewine's _POSIX Programmer's Guide_ (O'Reilly & Associates, 1991)
.\"     386BSD man pages
.\" Modified Sat Jul 24 18:08:52 1993 by Rik Faith (faith@cs.unc.edu)
.\" Modified 2001-08-31, aeb
.\"
.TH STRCMP 3  2020-04-11 "" "Linux Programmer's Manual"
.SH NAME
strcmp, strncmp \- compare two strings
.SH SYNOPSIS
.nf
.B #include <string.h>
.PP
.BI "int strcmp(const char *" s1 ", const char *" s2 );
.PP
.BI "int strncmp(const char *" s1 ", const char *" s2 ", size_t " n );
.fi
.SH DESCRIPTION
The
.BR strcmp ()
function compares the two strings
.I s1
and
.IR s2 .
The locale is not taken into account (for a locale-aware comparison, see
.BR strcoll (3)).
The comparison is done using unsigned characters.
.PP
.BR strcmp ()
returns an integer indicating the result of the comparison, as follows:
.IP \(bu 2
0, if the
.I s1
and
.I s2
are equal;
.IP \(bu
a negative value if
.I s1
is less than
.IR s2 ;
.IP \(bu
a positive value if
.I s1
is greater than
.IR s2 .
.PP
The
.BR strncmp ()
function is similar, except it compares
only the first (at most)
.IR n
bytes of
.I s1
and
.IR s2 .
.SH RETURN VALUE
The
.BR strcmp ()
and
.BR strncmp ()
functions return an integer
less than, equal to, or greater than zero if
.I s1
(or the first
.I n
bytes thereof) is found, respectively, to be less than, to
match, or be greater than
.IR s2 .
.SH ATTRIBUTES
For an explanation of the terms used in this section, see
.BR attributes (7).
.TS
allbox;
lbw19 lb lb
l l l.
Interface	Attribute	Value
T{
.BR strcmp (),
.BR strncmp ()
T}	Thread safety	MT-Safe
.TE
.SH CONFORMING TO
POSIX.1-2001, POSIX.1-2008, C89, C99, SVr4, 4.3BSD.
.SH NOTES
POSIX.1 specifies only that:
.RS
.PP
The sign of a nonzero return value shall be determined by the sign
of the difference between the values of the first pair of bytes
(both interpreted as type
.IR "unsigned char" )
that differ in the strings being compared.
.RE
.PP
In glibc, as in most other implementations,
the return value is the arithmetic result of subtracting
the last compared byte in
.I s2
from the last compared byte in
.IR s1 .
(If the two characters are equal, this difference is 0.)
.SH EXAMPLES
The program below can be used to demonstrate the operation of
.BR strcmp ()
(when given two arguments) and
.BR strncmp ()
(when given three arguments).
First, some examples using
.BR strcmp ():
.PP
.in +4n
.EX
$ \fB./string_comp ABC ABC\fP
<str1> and <str2> are equal
$ \fB./string_comp ABC AB\fP      # \(aqC\(aq is ASCII 67; \(aqC\(aq \- \(aq\0\(aq = 67
<str1> is greater than <str2> (67)
$ \fB./string_comp ABA ABZ\fP     # \(aqA\(aq is ASCII 65; \(aqZ\(aq is ASCII 90
<str1> is less than <str2> (\-25)
$ \fB./string_comp ABJ ABC\fP
<str1> is greater than <str2> (7)
$ .\fB/string_comp $\(aq\e201\(aq A\fP   # 0201 \- 0101 = 0100 (or 64 decimal)
<str1> is greater than <str2> (64)
.EE
.in
.PP
The last example uses
.BR bash (1)-specific
syntax to produce a string containing an 8-bit ASCII code;
the result demonstrates that the string comparison uses unsigned
characters.
.PP
And then some examples using
.BR strncmp ():
.PP
.in +4n
.EX
$ \fB./string_comp ABC AB 3\fP
<str1> is greater than <str2> (67)
$ \fB./string_comp ABC AB 2\fP
<str1> and <str2> are equal in the first 2 bytes
.EE
.in
.SS Program source
\&
.EX
/* string_comp.c

   Licensed under GNU General Public License v2 or later.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int
main(int argc, char *argv[])
{
    int res;

    if (argc < 3) {
        fprintf(stderr, "Usage: %s <str1> <str2> [<len>]\en", argv[0]);
        exit(EXIT_FAILURE);
    }

    if (argc == 3)
        res = strcmp(argv[1], argv[2]);
    else
        res = strncmp(argv[1], argv[2], atoi(argv[3]));

    if (res == 0) {
        printf("<str1> and <str2> are equal");
        if (argc > 3)
            printf(" in the first %d bytes\en", atoi(argv[3]));
        printf("\en");
    } else if (res < 0) {
        printf("<str1> is less than <str2> (%d)\en", res);
    } else {
        printf("<str1> is greater than <str2> (%d)\en", res);
    }

    exit(EXIT_SUCCESS);
}
.EE
.SH SEE ALSO
.BR bcmp (3),
.BR memcmp (3),
.BR strcasecmp (3),
.BR strcoll (3),
.BR string (3),
.BR strncasecmp (3),
.BR strverscmp (3),
.BR wcscmp (3),
.BR wcsncmp (3),
.BR ascii (7)
.SH COLOPHON
This page is part of release 5.09 of the Linux
.I man-pages
project.
A description of the project,
information about reporting bugs,
and the latest version of this page,
can be found at
\%https://www.kernel.org/doc/man\-pages/.