C String Functions
What You’ll Find Here
The C standard library provides dozens of string and memory functions in <string.h> for copying text, comparing names, searching substrings, tokenizing CSV lines, and handling locale-aware sorting. This page is your central hub: browse every function in our tutorial series, jump to full guides with examples and output, and learn patterns that work in real C programs.
Full Tutorials
30 functions with examples.
Quick Reference
All functions by category.
Searchable
Filter by function name.
Buffer Safety
Size and null-termination tips.
Beginner Tips
Standard vs extension notes.
Start Here
strlen() → strcpy().
Introduction
In C, strings are null-terminated arrays of char. You work with them through library functions such as strlen(), strcpy(), strcmp(), and strstr()—not methods on an object like C# or Java. Most live in <string.h>; locale helpers like strcoll() and strxfrm() also need <locale.h>.
C strings are low-level: you manage buffer sizes, null terminators, and whether a function reads or writes memory. Understanding that distinction prevents buffer overflows and undefined behavior.
Use writable arrays for mutating calls: char buf[64] = "hello";—never pass a string literal to strcpy, strcat, or strtok because literals are read-only.
String Functions Index
Search by function name or browse by category. Cards marked Tutorial link to full guides with syntax, five examples, output, and FAQs.
Memory Functions (mem*)
5 functionsOperate on raw memory blocks with a known byte count—useful for buffers, structs, and binary data as well as strings.
Copy & Concatenate
5 functionsCopy strings and append text—watch buffer sizes and null termination.
Length & Fill
4 functionsMeasure strings and fill or reverse buffers.
Compare & Collate
5 functionsLexicographic, bounded, case-insensitive, and locale-aware comparisons.
Search & Find
4 functionsLocate characters and substrings within null-terminated text.
Span, Tokenize & Parse
3 functionsMeasure character runs, skip delimiters, and split strings into tokens.
Case & Locale Transform
3 functionsChange letter case and build locale sort keys—some are compiler extensions.
Error Messages
1 functionsMap errno values to human-readable system error text.
🚀 Usage Tips
- Size your buffers — before
strcpyorstrcat, ensure the destination has room for existing text plus new bytes plus'\0'. - Prefer bounded copies — use
strncpyandstrncatwhen lengths are known; always null-terminate if the copy may fill the buffer. - Search smartly —
strchrfor one character,strstrfor substrings,strspn/strcspnfor parsing without destroying the string. - Tokenize carefully —
strtokmodifies the buffer and is not thread-safe; copy first or usestrtok_ron POSIX. - Know extensions —
strlwr,strupr,strrev, andstrdupare not ISO C; tutorials include portable alternatives. - Read the tutorial — each linked function page includes syntax, five examples, a quick reference table, and FAQs.
📝 How to Call a String Function
Every C string function follows the same general pattern:
#include <string.h>
return_type function_name(char *dest, const char *src, ...); Common Patterns
| Goal | Example |
|---|---|
| Get string length | strlen(text) |
| Copy safely with limit | strncpy(dest, src, sizeof(dest) - 1) |
| Compare for equality | strcmp(a, b) == 0 |
| Find substring | strstr(haystack, "needle") |
| Split on spaces | strtok(buf, " ") then strtok(NULL, " ") |
Functions vs Methods (C vs C#)
C has no String class. You call free functions with pointers. If you also learn C#, compare this page with our C# String Methods index—there you use dot notation (text.Contains("x")) on immutable string objects.
#include <stdio.h>
#include <string.h>
int main(void) {
char line[] = " Hello, C! ";
char *token;
size_t len = strlen(line);
printf("Length: %zu\n", len);
token = strtok(line, " ");
while (token != NULL) {
printf("Token: %s\n", token);
token = strtok(NULL, " ");
}
return 0;
} Conclusion
C string functions cover almost every text task in systems programming—from copying paths and comparing commands to searching logs and sorting names with locale rules. Use this index to find the right function quickly, then open the full tutorial for syntax, examples, and best practices.
Start with strlen() and strcpy() if you are new to C strings, then explore strcmp() and strstr() for comparison and search.
❓ Frequently Asked Questions
Tip: C string functions live in <string.h> and take char* or const char* arguments—for example strlen(text) or strcpy(dest, src). Many functions modify buffers in place (strcpy, strcat, strtok). Always ensure destination arrays are large enough and null-terminated.
Start Your First String Function Tutorial
Open the strlen() guide—syntax, five examples, and a quick reference cheat sheet.
29 people found this page helpful
