string.h: strcpy, strcat, strcmp & Friends in C
The <string.h> toolkit operates on null-terminated strings: strlen measures, strcpy/strcat copy and append (unbounded — the overflow classics), strcmp/strncmp compare (returning negative/zero/positive, not bool), and strchr/strstr search. Every function trusts the terminator — unterminated buffers send them marching into adjacent memory.
Because sizes are implicit, safe code pairs each call with explicit capacity knowledge: the destination must fit source plus terminator, verified by the programmer, not the library. Modern practice prefers the n-bounded variants or explicit length tracking.
Measure, copy, append
strlen(s) counts; strcpy(dst, src) copies; strcat(dst, src) appends — all assume dst has room, none check.
Compare three-way
strcmp(a, b) returns <0, 0, >0 — test == 0 for equality, never truthiness (0 means equal!).
Search with strchr/strstr
strchr finds a character, strstr finds a substring — both return pointers into the string or NULL.
Capacity is your job
No function knows dst's size — track it yourself and prefer strncpy/strncat or counted idioms.
#include <stdio.h>#include <string.h> int main(void) { char buf[16] = "hi"; strcat(buf, "!"); printf("%s\n", buf); printf("%d\n", strcmp(buf, "hi!")); return 0;}Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in C yourself — building beats watching.