1memcmp(3) Library Functions Manual memcmp(3)
2
3
4
6 memcmp - compare memory areas
7
9 Standard C library (libc, -lc)
10
12 #include <string.h>
13
14 int memcmp(const void s1[.n], const void s2[.n], size_t n);
15
17 The memcmp() function compares the first n bytes (each interpreted as
18 unsigned char) of the memory areas s1 and s2.
19
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
32 For an explanation of the terms used in this section, see at‐
33 tributes(7).
34
35 ┌────────────────────────────────────────────┬───────────────┬─────────┐
36 │Interface │ Attribute │ Value │
37 ├────────────────────────────────────────────┼───────────────┼─────────┤
38 │memcmp() │ Thread safety │ MT-Safe │
39 └────────────────────────────────────────────┴───────────────┴─────────┘
40
42 C11, POSIX.1-2008.
43
45 POSIX.1-2001, C89, SVr4, 4.3BSD.
46
48 Do not use memcmp() to compare confidential data, such as cryptographic
49 secrets, because the CPU time required for the comparison depends on
50 the contents of the addresses compared, this function is subject to
51 timing-based side-channel attacks. In such cases, a function that
52 performs comparisons in deterministic time, depending only on n (the
53 quantity of bytes compared) is required. Some operating systems
54 provide such a function (e.g., NetBSD's consttime_memequal()), but no
55 such function is specified in POSIX. On Linux, you may need to
56 implement such a function yourself.
57
59 bstring(3), strcasecmp(3), strcmp(3), strcoll(3), strncasecmp(3),
60 strncmp(3), wmemcmp(3)
61
62
63
64Linux man-pages 6.05 2023-07-30 memcmp(3)