memcmp(3)

๐Ÿ’– memcmp(3) โ€” memory compare, but make it bestie-coded ๐Ÿ’–

okay so real talk, this is memcmp from the Library Functions Manual, and honestly? she's an icon, she's a classic, and if you've ever compared two blobs of memory in C you've already met her. let's get into it ๐Ÿซถ


โœจ NAME โœจ

memcmp โ€” memory compare. that's it. that's the girl. she compares memory. ๐Ÿ’…


๐Ÿ“š LIBRARY ๐Ÿ“š

Standard C library (libc, -lc). she comes free with your libc, no cap. ๐ŸŒธ


๐ŸŽ€ SYNOPSIS ๐ŸŽ€

#include <string.h>  // see memory.h(3head)

int memcmp(size_t n;
           const void s1[n], const void s2[n], size_t n);

yes bestie, that's literally how it's written in the manual. we don't fix it, we don't question it, we just vibe. ๐Ÿฆ‹


๐Ÿ’… DESCRIPTION ๐Ÿ’…

The memcmp() function compares the first n bytes of the memory areas s1 and s2 โ€” and every single one of those bytes is interpreted as an unsigned char, okay? important detail. no sign shenanigans, no sneaky negative bytes, just pure unsigned-byte energy. ๐Ÿ‘‘


๐Ÿ”ฅ RETURN VALUE ๐Ÿ”ฅ

memcmp() hands you back an integer, and it's giving one of three moods:

  • less than zero โ€” if the first n bytes of s1 are found to be less than the first n bytes of s2 ๐Ÿ˜ญ
  • equal to zero โ€” if they match, slay ๐Ÿ’–
  • greater than zero โ€” if they're greater than the first n bytes of s2 ๐Ÿ”ฅ

for a nonzero return value, the sign is determined by the sign of the difference between the first pair of bytes (interpreted as unsigned char) that differ in s1 and s2. so like, it stops caring the moment it finds the difference, and that difference decides the whole vibe. โœจ

and if n is zero? return value is zero. nothing compared, nothing to report, we go home. ๐Ÿ’€


๐ŸŒˆ ATTRIBUTES ๐ŸŒˆ

for an explanation of the terms used in this section, go check out attributes(7). and the tea is:

  • memcmp()
    • Thread safety: MT-Safe โ€” she's safe to bring around your other threads, no drama, no chaos, we love a stable queen ๐Ÿ‘‘

โญ VERSIONS โญ

so POSIX.1 only specifies this much, and I quote:

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 unsigned char) that differ in the strings being compared.

that's it. that's all POSIX promises you. ๐Ÿ˜ญ

but here's the actual tea: in glibc, as in most other implementations, the return value is the arithmetic result of subtracting the last compared byte in s2 from the last compared byte in s1. (if the two characters are equal, this difference is 0.) so yes, it's lowkey just subtraction wearing a fancy manpage outfit. ๐Ÿ’…


๐Ÿ‘‘ STANDARDS ๐Ÿ‘‘

C11, POSIX.1-2008. certified, standardized, periodt. โœจ


๐Ÿงš HISTORY ๐Ÿงš

POSIX.1-2001, C89, SVr4, 4.3BSD. she's been around since forever, an OG, a legacy legend. ๐Ÿซถ


โš ๏ธ CAVEATS โš ๏ธ

okay bestie, this is the part where you sit down and listen, because this one is important:

do NOT use memcmp() to compare confidential data, like cryptographic secrets. ๐Ÿ™ˆ

why? because the CPU time required for the comparison depends on the contents of the addresses compared โ€” and that means this function is subject to timing-based side-channel attacks. someone can literally clock how long your comparison took and learn stuff about your secret. absolutely not. ๐Ÿ’€

in those cases, what you actually need is a function that performs comparisons in deterministic time, depending only on n (the quantity of bytes compared). some operating systems provide such a function โ€” for example, NetBSD's consttime_memequal() โ€” but no such function is specified in POSIX. ๐Ÿ˜ญ

on Linux? you may need to implement such a function yourself. yeah. bestie. you. yourself. I know. I'm sorry. ๐Ÿซถ


๐ŸŒธ SEE ALSO ๐ŸŒธ


and that's the whole page, bestie!! this was pulled straight from Linux man-pages 6.20, dated 2026-08-10 โ€” so you know it's fresh, you know it's official, and you know it's got main character energy. go compare some bytes responsibly ๐Ÿ’–โœจ๐ŸŽ€

๐Ÿ“ฆ where this page comes from
  • arch rolling: man-pages 6.20-1 ยท BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
  • nixpkgs unstable: man-pages 6.20
  • ๐Ÿ“œ the page's license: Linux-man-pages-copyleft
  • ๐Ÿ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms