1memcmp(3)                  Library Functions Manual                  memcmp(3)
2
3
4

NAME

6       memcmp - compare memory areas
7

LIBRARY

9       Standard C library (libc, -lc)
10

SYNOPSIS

12       #include <string.h>
13
14       int memcmp(const void s1[.n], const void s2[.n], size_t n);
15

DESCRIPTION

17       The  memcmp()  function compares the first n bytes (each interpreted as
18       unsigned char) of the memory areas s1 and s2.
19

RETURN VALUE

21       The memcmp() function returns  an  integer  less  than,  equal  to,  or
22       greater than zero if the first n bytes of s1 is found, respectively, to
23       be less than, to match, or be greater than the first n bytes of s2.
24
25       For a nonzero return value, the sign is determined by the sign  of  the
26       difference  between  the  first  pair of bytes (interpreted as unsigned
27       char) that differ in s1 and s2.
28
29       If n is zero, the return value is zero.
30

ATTRIBUTES

32       For an  explanation  of  the  terms  used  in  this  section,  see  at‐
33       tributes(7).
34
35       ┌────────────────────────────────────────────┬───────────────┬─────────┐
36Interface                                   Attribute     Value   
37       ├────────────────────────────────────────────┼───────────────┼─────────┤
38memcmp()                                    │ Thread safety │ MT-Safe │
39       └────────────────────────────────────────────┴───────────────┴─────────┘
40

STANDARDS

42       C11, POSIX.1-2008.
43

HISTORY

45       POSIX.1-2001, C89, SVr4, 4.3BSD.
46

CAVEATS

48       Do  not use memcmp() to compare security critical data, such as crypto‐
49       graphic secrets, because the required CPU time depends on the number of
50       equal bytes.  Instead, a function that performs comparisons in constant
51       time is required.  Some  operating  systems  provide  such  a  function
52       (e.g.,  NetBSD's  consttime_memequal()), but no such function is speci‐
53       fied in POSIX.  On Linux, you may need to  implement  such  a  function
54       yourself.
55

SEE ALSO

57       bstring(3),   strcasecmp(3),   strcmp(3),  strcoll(3),  strncasecmp(3),
58       strncmp(3), wmemcmp(3)
59
60
61
62Linux man-pages 6.04              2023-03-30                         memcmp(3)
Impressum