<string.h> is the C standard header for null-terminated strings and raw memory helpers. A C string is a sequence of characters ending with '\0'. Functions like strlen, strcpy, strcmp, and strstr assume that layout.
Remember
#include <string.h>
"hi" in memory → 'h' 'i' '\0'
strlen("hi") → 2 (does not count '\0')
strcpy / strcat → need enough room in dest
strcmp(a, b) == 0 → equal (not a == b)
strchr / strstr → pointer or NULL
also: memcpy, memset, memmove (byte APIs)
Size the destination first, keep the null terminator, and compare with strcmp—not ==.
Include <string.h> (and <stdio.h> if you print).
Give destination arrays enough room for characters plus'\0'.
Copy or append with strcpy/strcat (or bounded variants).
Compare with strcmp; search with strchr/strstr and check for NULL.
Piece
Required?
Role
#include <string.h>
Yes
Declares string and memory functions
Null terminator
Yes
Marks the end of every C string
Buffer size
Yes for writes
Must fit text + '\0'
Pseudocode
Pseudocode
ensure dest has capacity for src (+ existing text if strcat)
strcpy(dest, src) // copy including '\0'
len = strlen(dest) // count until '\0'
if strcmp(a, b) == 0:
strings are equal
else if strcmp(a, b) < 0:
a sorts before b
p = strstr(haystack, needle)
if p != NULL:
found at p
else:
not found
Reference
Cheat Sheet
Core string APIs you will use constantly.
Function
Action / return
strlen(s)
Length excluding '\0'
strcpy(d, s)
Copy s into d
strncpy(d, s, n)
Copy at most n chars (may omit '\0')
strcat(d, s)
Append s onto d
strcmp(a, b)
<0 / 0 / >0
strchr(s, c)
First char c, or NULL
strstr(h, n)
First substring, or NULL
memset / memcpy
Fill / copy raw bytes
Try it
Live Preview
Pick an operation and see the result for sample strings "Hello, " and "World!".
Browser simulation of common string.h calls (same idea as the C examples below).
Result
s = "Hello, World!"
strlen(s) → 13
(does not count the final '\0')
Trace
Worked Walkthrough — Build "Hello, World!"
Trace copy, append, length, and compare on two short strings.
str3: World!
str1: Hello, World!
Length of str1: 13
How It Works
Copy.strcpy(str3, str2) writes "World!" plus '\0' into str3.
Append.strcat finds the end of "Hello, " and adds "World!".
Measure.strlen counts 13 characters before the terminator.
Capacity.str1[32] is large enough for 13 chars + '\0'—always check this before writing.
Example 2 — Compare with strcmp
Pointer equality (a == b) is not string equality. Use strcmp.
C
#include <stdio.h>
#include <string.h>
int main(void) {
char a[] = "Hello";
char b[] = "World";
char c[] = "Hello";
printf("strcmp(a, b): %d\n", strcmp(a, b));
printf("strcmp(a, c): %d\n", strcmp(a, c));
if (strcmp(a, b) < 0) {
printf("\"Hello\" comes before \"World\"\n");
}
return 0;
}
Output
strcmp(a, b): -15
strcmp(a, c): 0
"Hello" comes before "World"
How It Works
Compare bytes.strcmp walks both strings until a difference or '\0'.
Unequal.'H' vs 'W' → negative result (exact number is implementation-defined).
Equal.a and c match → return 0.
Test the sign. Use < 0, == 0, or > 0—not equality with -1 or 1.
Example 3 — Find with strchr and strstr
Locate a character or a substring; always check for NULL.
C
#include <stdio.h>
#include <string.h>
int main(void) {
const char *text = "error: file missing";
char *comma;
char *word;
comma = strchr(text, ':');
word = strstr(text, "file");
if (comma != NULL) {
printf("Colon at index %td\n", comma - text);
}
if (word != NULL) {
printf("Found: %s\n", word);
}
return 0;
}
Output
Colon at index 5
Found: file missing
How It Works
strchr. Scans for ':' and returns a pointer to that byte (index 5).
strstr. Scans for the substring "file" and returns a pointer into text.
Null check. If either search fails, the pointer is NULL—do not dereference it.
Print from match. Printing word shows the rest of the string from that point. Pair with ctype.h when you classify characters while scanning.
Edge Cases & Pitfalls
Most string bugs are buffer and terminator mistakes.
overflow
Destination must be large enough
strcpy/strcat do not check size. Overflow is undefined behavior.
strncpy
strncpy may omit '\0'
If n cuts mid-string, add the terminator yourself, or use snprintf.
compare
Do not use == for text
a == b compares pointers. Use strcmp(a, b) == 0 for contents.
null
Never pass NULL to strlen/strcpy
These functions require valid strings. Passing NULL is undefined behavior.
Analysis
Performance Notes
Approach
Cost
Best for
strlen / strcmp
O(n) over characters
Normal short strings
strcpy / strcat
O(n) copy
When dest size is known
Repeated strlen in a loop
Can add up
Cache the length once
For tiny strings, clarity wins. For large buffers, prefer bounded APIs and avoid rescanning the same string with strlen on every iteration.
Remember
Key Takeaways
Shape: C strings end with '\0'.
Safety: size destinations for text + terminator.
Compare:strcmp for contents, not ==.
Search:strchr/strstr return NULL on miss.
One line:string.h works on null-terminated text—know the buffer size before you write.
Frequently Asked Questions
string.h is the C standard header for null-terminated character arrays and raw memory blocks. It provides strlen, strcpy, strcat, strcmp, strncpy, strchr, strstr, memcpy, memset, and related functions used in almost every C program that handles text.
strcpy copies until the source null terminator—if the destination is too small, you get a buffer overflow. strncpy copies at most n bytes but may not add a null terminator if the source is longer than n. After strncpy, set dest[n-1] = '\0' or prefer snprintf for safer copies.
strcmp compares two strings lexicographically. It returns 0 if equal, negative if the first sorts before the second, and positive if after. Do not assume the values are exactly -1 or 1—check < 0, == 0, or > 0.
No. strlen returns the number of characters before the terminating '\0'. The string "hi" has length 2, so the array needs at least 3 bytes including the null.
strcat appends without checking remaining space. The destination must hold the existing text, the new text, and the final '\0'. Prefer strncat with a limit or snprintf to build strings safely.
strchr finds the first occurrence of a single character. strstr finds the first occurrence of a whole substring. Both return a pointer to the match or NULL if not found.