The string.h Library
<string.h>, packed with functions for working with null-terminated strings: measuring length, copying, concatenating, comparing, and searching. Because C strings are just char arrays with a convention (see Strings as Char Arrays), all of these functions operate by scanning memory for a '\0' byte — which is exactly why several of them are dangerous if you are not careful about buffer sizes.Including the Header
#include <string.h>
Key Functions at a Glance
Function | What it does |
|---|---|
| Returns the number of characters in |
| Copies |
| Copies at most |
| Appends |
| Appends at most |
| Compares two strings lexicographically; returns 0 if equal, negative if |
| Like |
| Returns a pointer to the first occurrence of character |
| Returns a pointer to the first occurrence of substring |
| Splits |
Bounds Checking Is Mostly Your Job
strcpy and strcat will happily write past the end of a destination buffer if the source content doesn't fit — they have no way to know how big dest actually is. This has made them the source of countless real-world security vulnerabilities over the decades. Bounded variants like strncpy and strncat exist to limit how much gets written, but they come with their own subtleties — for example, strncpy does not guarantee the result is null-terminated if the source is too long. Bounded does not automatically mean safe; you still need to understand exactly what each function guarantees. The next page walks through worked examples of these functions and their pitfalls in detail.<string.h>provides functions for length, copying, concatenation, comparison, and searching on C stringsAll of these functions work by scanning for a null terminator, just like
printf("%s", ...)strcpyandstrcathave no bounds checking -- classic sources of buffer overflowsThe bounded
n-variants (strncpy,strncat,strncmp) limit how much is read or written, but still require careUnderstanding what each function actually guarantees (especially around null-termination) matters more than just picking the "safe-sounding" version