string.h
<string.h> is the C library header for byte-string and raw-memory operations: copy, compare, search, and fill. Every function that takes a string assumes a '\0' terminator unless its name starts with mem.
Detailed Explanation
Split the header into two families.
The str* functions treat arguments as C strings. strlen(s) counts bytes before '\0'. strcmp and strncmp compare lexicographically as unsigned char values and return negative, zero, or positive. strchr and strrchr find a character. strstr finds a substring. strcpy and strcat copy and append with no bound. strncpy and strncat take a bound and have sharp edges: strncpy may skip the terminator, strncat uses its n as "bytes of source to consider," not "bytes of destination left."
The mem* functions treat arguments as raw bytes and take an explicit length. memcpy copies n bytes that must not overlap. memmove copies n bytes that may overlap. memcmp compares n bytes. memchr finds a byte. memset fills n bytes with one value. These are the right tools for structs, buffers that may contain interior '\0', and anything that is not text.
C11 added a set of _s "bounds-checking" functions (strcpy_s, and so on). They are optional, unevenly implemented, and not a substitute for passing a capacity you already know. POSIX adds strdup, strndup, strlcpy, and strlcat. strdup is in C23. strlcpy is still a BSD/macOS function and is absent from glibc; snprintf(dest, dest_sz, "%s", src) is the portable bound copy.
Error handling is thin. None of these functions set errno on a missing terminator, because they cannot know you forgot one. They will read or write until they find '\0' or until the process dies. NULL is not a valid string argument. Passing it is undefined behavior, not a documented no-op.
Pick the function by the invariant you have. If you have a terminator and a destination capacity, snprintf or a well-reviewed strlcpy polyfill. If you have a byte count, memcpy / memmove. If you are comparing passwords or MACs, do not use strcmp; use a constant-time compare. If you are comparing text for humans, this header is not a Unicode library.
Code Examples
#include <stdio.h>
#include <string.h>
int main(void)
{
char dest[8];
const char *src = "code";
/* Portable bounded copy that always terminates when dest is non-empty. */
snprintf(dest, sizeof dest, "%s", src);
printf("%s %zu\n", dest, strlen(dest)); /* prints: code 4 */
if (strcmp(dest, "code") == 0) {
printf("match\n"); /* prints: match */
}
return 0;
}#include <stdio.h>
#include <string.h>
int main(void)
{
char raw[6] = { 'a', 'b', '\0', 'c', 'd', 'e' };
printf("%zu\n", strlen(raw)); /* prints: 2 — stops at the first '\0' */
printf("%d\n", memcmp(raw, "ab\0cd", 5)); /* prints: 0 — compares five bytes */
memmove(raw + 1, raw, 3); /* overlap: use memmove, not memcpy */
/* raw bytes now: a a b \0 d e */
printf("%d\n", raw[1] == 'a' && raw[2] == 'b'); /* prints: 1 */
return 0;
}Common Gotchas
`strcat(dest, src)` needs room for `strlen(dest) + strlen(src) + 1`
it starts writing at the existing terminator. An overflow here is a classic exploit primitive.
`strncpy` is not "strcpy with a bound"
it pads with zeros when `src` is short and omits `'\0'` when `src` is long. That second case is why style guides ban it.
`memcpy` on overlapping regions is undefined behavior
use `memmove` when the source and destination may share bytes.
`strdup` allocates; you must `free` the result
a `NULL` return is out-of-memory, not an empty string.