1STRCMP(3) Linux Programmer's Manual STRCMP(3)
2
3
4
6 strcmp, strncmp - compare two strings
7
9 #include <string.h>
10
11 int strcmp(const char *s1, const char *s2);
12
13 int strncmp(const char *s1, const char *s2, size_t n);
14
16 The strcmp() function compares the two strings s1 and s2. The locale
17 is not taken into account (for a locale-aware comparison, see str‐
18 coll(3)). The comparison is done using unsigned characters.
19
20 strcmp() returns an integer indicating the result of the comparison, as
21 follows:
22
23 • 0, if the s1 and s2 are equal;
24
25 • a negative value if s1 is less than s2;
26
27 • a positive value if s1 is greater than s2.
28
29 The strncmp() function is similar, except it compares only the first
30 (at most) n bytes of s1 and s2.
31
33 The strcmp() and strncmp() functions return an integer less than, equal
34 to, or greater than zero if s1 (or the first n bytes thereof) is found,
35 respectively, to be less than, to match, or be greater than s2.
36
38 For an explanation of the terms used in this section, see at‐
39 tributes(7).
40
41 ┌────────────────────┬───────────────┬─────────┐
42 │Interface │ Attribute │ Value │
43 ├────────────────────┼───────────────┼─────────┤
44 │strcmp(), strncmp() │ Thread safety │ MT-Safe │
45 └────────────────────┴───────────────┴─────────┘
47 POSIX.1-2001, POSIX.1-2008, C89, C99, SVr4, 4.3BSD.
48
50 POSIX.1 specifies only that:
51
52 The sign of a nonzero return value shall be determined by the
53 sign of the difference between the values of the first pair of
54 bytes (both interpreted as type unsigned char) that differ in
55 the strings being compared.
56
57 In glibc, as in most other implementations, the return value is the
58 arithmetic result of subtracting the last compared byte in s2 from the
59 last compared byte in s1. (If the two characters are equal, this dif‐
60 ference is 0.)
61
63 The program below can be used to demonstrate the operation of strcmp()
64 (when given two arguments) and strncmp() (when given three arguments).
65 First, some examples using strcmp():
66
67 $ ./string_comp ABC ABC
68 <str1> and <str2> are equal
69 $ ./string_comp ABC AB # 'C' is ASCII 67; 'C' - ' ' = 67
70 <str1> is greater than <str2> (67)
71 $ ./string_comp ABA ABZ # 'A' is ASCII 65; 'Z' is ASCII 90
72 <str1> is less than <str2> (-25)
73 $ ./string_comp ABJ ABC
74 <str1> is greater than <str2> (7)
75 $ ./string_comp $'\201' A # 0201 - 0101 = 0100 (or 64 decimal)
76 <str1> is greater than <str2> (64)
77
78 The last example uses bash(1)-specific syntax to produce a string con‐
79 taining an 8-bit ASCII code; the result demonstrates that the string
80 comparison uses unsigned characters.
81
82 And then some examples using strncmp():
83
84 $ ./string_comp ABC AB 3
85 <str1> is greater than <str2> (67)
86 $ ./string_comp ABC AB 2
87 <str1> and <str2> are equal in the first 2 bytes
88
89 Program source
90
91 /* string_comp.c
92
93 Licensed under GNU General Public License v2 or later.
94 */
95 #include <stdio.h>
96 #include <stdlib.h>
97 #include <string.h>
98
99 int
100 main(int argc, char *argv[])
101 {
102 int res;
103
104 if (argc < 3) {
105 fprintf(stderr, "Usage: %s <str1> <str2> [<len>]\n", argv[0]);
106 exit(EXIT_FAILURE);
107 }
108
109 if (argc == 3)
110 res = strcmp(argv[1], argv[2]);
111 else
112 res = strncmp(argv[1], argv[2], atoi(argv[3]));
113
114 if (res == 0) {
115 printf("<str1> and <str2> are equal");
116 if (argc > 3)
117 printf(" in the first %d bytes\n", atoi(argv[3]));
118 printf("\n");
119 } else if (res < 0) {
120 printf("<str1> is less than <str2> (%d)\n", res);
121 } else {
122 printf("<str1> is greater than <str2> (%d)\n", res);
123 }
124
125 exit(EXIT_SUCCESS);
126 }
127
129 bcmp(3), memcmp(3), strcasecmp(3), strcoll(3), string(3), strn‐
130 casecmp(3), strverscmp(3), wcscmp(3), wcsncmp(3), ascii(7)
131
133 This page is part of release 5.10 of the Linux man-pages project. A
134 description of the project, information about reporting bugs, and the
135 latest version of this page, can be found at
136 https://www.kernel.org/doc/man-pages/.
137
138
139
140 2020-04-11 STRCMP(3)