C String Functions

Beginner
⏱️ 14 min read
📚 Updated: Jul 2026
🎯 30 Tutorials
<string.h>

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.

01

Full Tutorials

30 functions with examples.

02

Quick Reference

All functions by category.

03

Searchable

Filter by function name.

04

Buffer Safety

Size and null-termination tips.

05

Beginner Tips

Standard vs extension notes.

06

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.

💡
Beginner Tip

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 functions

Operate on raw memory blocks with a known byte count—useful for buffers, structs, and binary data as well as strings.

Copy & Concatenate

5 functions

Copy strings and append text—watch buffer sizes and null termination.

Length & Fill

4 functions

Measure strings and fill or reverse buffers.

Compare & Collate

5 functions

Lexicographic, bounded, case-insensitive, and locale-aware comparisons.

Span, Tokenize & Parse

3 functions

Measure character runs, skip delimiters, and split strings into tokens.

Case & Locale Transform

3 functions

Change letter case and build locale sort keys—some are compiler extensions.

Error Messages

1 functions

Map errno values to human-readable system error text.

🚀 Usage Tips

  • Size your buffers — before strcpy or strcat, ensure the destination has room for existing text plus new bytes plus '\0'.
  • Prefer bounded copies — use strncpy and strncat when lengths are known; always null-terminate if the copy may fill the buffer.
  • Search smartlystrchr for one character, strstr for substrings, strspn/strcspn for parsing without destroying the string.
  • Tokenize carefullystrtok modifies the buffer and is not thread-safe; copy first or use strtok_r on POSIX.
  • Know extensionsstrlwr, strupr, strrev, and strdup are 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

GoalExample
Get string lengthstrlen(text)
Copy safely with limitstrncpy(dest, src, sizeof(dest) - 1)
Compare for equalitystrcmp(a, b) == 0
Find substringstrstr(haystack, "needle")
Split on spacesstrtok(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

C string functions are library routines in string.h for working with null-terminated char arrays: measuring length, copying, comparing, searching, tokenizing, and transforming text. You call them by name with pointers, such as strlen(s) or strstr(haystack, needle).
Include the right header (usually string.h), then call function_name(arguments). Example: char buf[32]; strcpy(buf, "hello"); size_t n = strlen(buf); Always pass writable buffers to functions that modify strings.
strcpy() copies until it hits the source null terminator and requires dest to be big enough—it is easy to overflow. strncpy() copies at most n bytes and may leave dest without a null terminator if the limit cuts the copy short. Prefer strncpy or safer patterns when size is bounded.
strcmp() compares bytes exactly (case-sensitive). strcasecmp() (POSIX) or _stricmp() (Windows) ignores letter case. Use strcmp for identifiers; strcasecmp for user-facing fuzzy matching when your platform provides it.
Some do, some do not. strcpy(), strcat(), strtok(), strlwr(), and strupr() modify memory. strlen(), strcmp(), strchr(), and strstr() only read. Check each function's tutorial—never pass string literals to mutating functions.
Start with strlen() and strcpy() to understand length and copying, then strcmp() for equality checks, and strchr() or strstr() for search. Those five cover the basics before span functions (strspn/strcspn) and tokenizing (strtok).
Did you know?

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.

strlen() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

29 people found this page helpful