C String Functions
Since C strings are plain char arrays, you cannot use =, + or == on them. Instead, the standard header <string.h> provides a family of functions to measure, copy, join, compare and search strings. Include it at the top of your file:
#include <string.h>The essential functions
| Function | What it does |
|---|---|
strlen(s) |
number of characters before the '\0' |
strcpy(dest, src) |
copies src (including '\0') into dest |
strncpy(dest, src, n) |
copies at most n characters |
strcat(dest, src) |
appends src to the end of dest |
strncat(dest, src, n) |
appends at most n characters |
strcmp(a, b) |
compares: 0 if equal, negative if a sorts first, positive otherwise |
strncmp(a, b, n) |
compares only the first n characters |
strchr(s, c) |
pointer to the first occurrence of character c, or NULL |
strstr(s, sub) |
pointer to the first occurrence of substring sub, or NULL |
Length: strlen
strlen counts characters up to (not including) the terminator. Compare it with sizeof, which measures the whole array:
char name[32] = "Margaret";
strlen(name); // 8 - characters before the terminator
sizeof(name); // 32 - size of the whole array
strlen(""); // 0 - the empty stringstrlen returns a size_t, printed with %zu.
Copying and joining: strcpy and strcat
Because arrays cannot be assigned, strcpy copies text into an existing array. strcat appends to the end of what is already there:
#include <stdio.h>
#include <string.h>
int main(void) {
char full[40];
strcpy(full, "Ada");
strcat(full, " ");
strcat(full, "Lovelace");
printf("Full name: %s (%zu chars)\n", full, strlen(full));
char backup[40];
strcpy(backup, full);
full[0] = 'X';
printf("full: %s, backup: %s\n", full, backup);
return 0;
}Full name: Ada Lovelace (12 chars)
full: Xda Lovelace, backup: Ada Lovelace
Warning: Neither
strcpynorstrcatknows how bigdestis. If the result does not fit, they write past the end of the array — a buffer overflow, the cause of countless security bugs. Always make sure the destination is large enough.
A safe and convenient alternative is snprintf, which formats into a buffer and never writes more than the size you give it:
#include <stdio.h>
int main(void) {
char label[12];
int n = snprintf(label, sizeof label, "%s-%d", "invoice", 20250);
printf("label: %s\n", label);
printf("needed %d chars, buffer holds %zu\n", n, sizeof label - 1);
return 0;
}label: invoice-202
needed 13 chars, buffer holds 11
The text was cut off safely, and the return value tells you how long it would have been, so you can detect truncation.
Comparing: strcmp
strcmp compares two strings character by character. It returns 0 when they are equal — easy to get backwards, since 0 usually means false:
#include <stdio.h>
#include <string.h>
int main(void) {
const char *a = "apple";
const char *b = "apricot";
const char *c = "apple";
printf("a vs c: %s\n", strcmp(a, c) == 0 ? "equal" : "different");
printf("a vs b: %s\n", strcmp(a, b) < 0 ? "a sorts first" : "b sorts first");
printf("\"Zoo\" vs \"apple\": %s\n", strcmp("Zoo", "apple") < 0 ? "Zoo first" : "apple first");
printf("first 2 chars of a and b equal? %s\n", strncmp(a, b, 2) == 0 ? "yes" : "no");
return 0;
}a vs c: equal
a vs b: a sorts first
"Zoo" vs "apple": Zoo first
first 2 chars of a and b equal? yes
Comparison uses character codes, so all uppercase letters (65–90) sort before lowercase ones (97–122). That is why "Zoo" comes before "apple".
Searching: strchr and strstr
These return a pointer into the original string where the match starts, or NULL if there is none. Subtracting the start of the string gives the index:
#include <stdio.h>
#include <string.h>
int main(void) {
const char *email = "grace.hopper@navy.mil";
const char *at = strchr(email, '@');
if (at != NULL) {
printf("'@' found at index %td\n", at - email);
printf("domain: %s\n", at + 1);
}
const char *found = strstr(email, "hopper");
printf("contains \"hopper\": %s\n", found ? "yes" : "no");
printf("contains \"turing\": %s\n", strstr(email, "turing") ? "yes" : "no");
return 0;
}'@' found at index 12
domain: navy.mil
contains "hopper": yes
contains "turing": no
%td prints a ptrdiff_t, the type you get when subtracting two pointers.
Character helpers in <ctype.h>
For single characters, <ctype.h> offers tests and conversions such as isalpha, isdigit, isspace, toupper and tolower:
#include <stdio.h>
#include <ctype.h>
int main(void) {
char text[] = "Hello, World 2025!";
int letters = 0, digits = 0;
for (int i = 0; text[i] != '\0'; i++) {
if (isalpha((unsigned char) text[i])) letters++;
if (isdigit((unsigned char) text[i])) digits++;
text[i] = toupper((unsigned char) text[i]);
}
printf("%s\nletters=%d digits=%d\n", text, letters, digits);
return 0;
}HELLO, WORLD 2025!
letters=10 digits=4
Tip: Cast to
unsigned charbefore passing acharto<ctype.h>functions; negative values (non-ASCII bytes) are otherwise undefined behaviour.
Go further: C Buffers, Lengths and Unsafe Functions reviews
strcpy,strcat,sprintfandgetsas a security reviewer and shows the bounded replacements.
Test yourself
What does strcmp("cat", "cat") return?
- 1
- 0
- true
- -1
What is strlen("hello")?
- 4
- 5
- 6
- It depends on the array size
Why is strcpy(dest, src) dangerous?
- It deletes
src - It cannot copy spaces
- It does not check whether
destis big enough - It only copies the first character