On this page
memcmp
Prev: memset | Up: string.h | Next: memchr
Rel: string.h, stdlib.h & string.h Guide
Ref: cppreference — memcmp
Tags: #public #prog #c #stdlib #string #tier/core
Include
#include <string.h>
Signature & Overview
int memcmp(const void *lhs, const void *rhs, size_t n);
Compares n raw bytes as unsigned char; returns <0, 0, or >0.
Notes & Pitfalls
- Not constant-time: Never use
memcmp()to compare cryptographic HMACs or tokens (timing side channel); also watch for uninitialized struct padding bytes.
Example
#include <assert.h>
#include <string.h>
int main(void) {
assert(memcmp("abc", "abc", 3) == 0);
assert(memcmp("abc", "abd", 3) < 0);
}
Linked from
Related notes
- tuts/c/stdlib/string/memchr Linked here; links to this page; shared tags: #c, #prog, #stdlib, #string, #tier
- tuts/c/stdlib/string/memset Linked here; links to this page; shared tags: #c, #prog, #stdlib, #string, #tier
- tuts/c/stdlib/string Linked here; links to this page; shared tags: #c, #prog, #stdlib, #string
- tuts/c/stdlib/stdlib_and_string_h Linked here; shared tags: #c, #prog, #stdlib
- tuts/c/stdlib/string/memcpy shared tags: #c, #prog, #stdlib, #string, #tier
- tuts/c/stdlib/string/memmove shared tags: #c, #prog, #stdlib, #string, #tier
- tuts/c/stdlib/string/strcat shared tags: #c, #prog, #stdlib, #string, #tier
- tuts/c/stdlib/string/strchr shared tags: #c, #prog, #stdlib, #string, #tier
- tuts/c/stdlib/string/strcmp shared tags: #c, #prog, #stdlib, #string, #tier
- tuts/c/stdlib/string/strcpy shared tags: #c, #prog, #stdlib, #string, #tier