1STRCMP(3)                  Linux Programmer's Manual                 STRCMP(3)
2
3
4

NAME

6       strcmp, strncmp - compare two strings
7

SYNOPSIS

9       #include <string.h>
10
11       int strcmp(const char *s1, const char *s2);
12       int strncmp(const char *s1, const char *s2, size_t n);
13

DESCRIPTION

15       The  strcmp()  function compares the two strings s1 and s2.  The locale
16       is not taken into account (for  a  locale-aware  comparison,  see  str‐
17       coll(3)).  The comparison is done using unsigned characters.
18
19       strcmp() returns an integer indicating the result of the comparison, as
20       follows:
21
22       • 0, if the s1 and s2 are equal;
23
24       • a negative value if s1 is less than s2;
25
26       • a positive value if s1 is greater than s2.
27
28       The strncmp() function is similar, except it compares  only  the  first
29       (at most) n bytes of s1 and s2.
30

RETURN VALUE

32       The strcmp() and strncmp() functions return an integer less than, equal
33       to, or greater than zero if s1 (or the first n bytes thereof) is found,
34       respectively, to be less than, to match, or be greater than s2.
35

ATTRIBUTES

37       For  an  explanation  of  the  terms  used  in  this  section,  see at‐
38       tributes(7).
39
40       ┌────────────────────────────────────────────┬───────────────┬─────────┐
41Interface                                   Attribute     Value   
42       ├────────────────────────────────────────────┼───────────────┼─────────┤
43strcmp(), strncmp()                         │ Thread safety │ MT-Safe │
44       └────────────────────────────────────────────┴───────────────┴─────────┘
45

CONFORMING TO

47       POSIX.1-2001, POSIX.1-2008, C89, C99, SVr4, 4.3BSD.
48

NOTES

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

EXAMPLES

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' - '\0' = 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

SEE ALSO

129       bcmp(3),  memcmp(3),  strcasecmp(3),   strcoll(3),   string(3),   strn‐
130       casecmp(3), strverscmp(3), wcscmp(3), wcsncmp(3), ascii(7)
131

COLOPHON

133       This  page  is  part of release 5.12 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                                  2021-03-22                         STRCMP(3)
Impressum