Learn C
#include "arraysstrings/string_functions.h"

string.h: strcpy, strcat, strcmp & Friends in C

#concept

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.

#how it works
1

Measure, copy, append

strlen(s) counts; strcpy(dst, src) copies; strcat(dst, src) appends — all assume dst has room, none check.

2

Compare three-way

strcmp(a, b) returns <0, 0, >0 — test == 0 for equality, never truthiness (0 means equal!).

3

Search with strchr/strstr

strchr finds a character, strstr finds a substring — both return pointers into the string or NULL.

4

Capacity is your job

No function knows dst's size — track it yourself and prefer strncpy/strncat or counted idioms.

#example
#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;
}
Output
hi!
0
1buf starts as hi plus terminator in 16 bytes — ample room for one more character.
2strcat appends ! before the terminator and re-terminates; hi! prints.
3strcmp against the same text returns 0 — equality reads as zero, the classic gotcha.
#use cases
Copying and appending with manually verified destination capacity
Equality tests via strcmp(...) == 0, never bare truthiness
Searching text with strchr/strstr pointer results
Auditing every strcpy/strcat call for buffer sizing

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in C yourself — building beats watching.