Strings hold almost every piece of text in C—user names, file paths, command-line arguments, and log lines. This guide teaches how C represents strings as null-terminated char arrays, how to create them safely, and how to use essential <string.h> helpers.
01
char Arrays
Text in C.
02
Null \0
End marker.
03
string.h
Library helpers.
04
strlen strcpy
Length & copy.
05
Search & split
strstr strtok.
06
29 Tutorials
Function index.
Fundamentals
Definition and Usage
In C, a string is not a separate type. It is a char array whose last meaningful character is followed by a null terminator '\0' (byte value 0). The standard library treats any such sequence as text.
You declare writable strings with arrays: char greeting[] = "Hello, C!"; You read from literals with pointers: const char *label = "CodeToFun"; Functions in <string.h> measure, copy, compare, search, and tokenize these character sequences.
💡
Beginner Tip
Always leave room for the null byte. A buffer declared as char word[6] can hold at most five visible letters plus '\0'. When copying, ensure the destination is large enough—buffer overflows are a classic C bug.
Foundation
📝 Syntax
Common ways to create and use strings in C:
C
char buffer[] = "Hello, C!"; // writable array
const char *label = "CodeToFun"; // read-only literal
size_t len = strlen(buffer); // length (exclude \0)
strcpy(dest, src); // copy (dest must be big enough)
int cmp = strcmp(a, b); // compare bytes
Work through these five examples from basic string creation to comparison, concatenation, and tokenization patterns used in real C programs.
📚 Getting Started
Create your first strings and print them with printf.
Example 1 — Create and Display Strings
Assign text to a char array and print it—the foundation of every C program that handles text.
C
#include <stdio.h>
int main(void) {
char language[] = "C";
const char *site = "CodeToFun";
printf("Language: %s\n", language);
printf("Site: %s\n", site);
printf("Ready to learn C strings!\n");
return 0;
}
📤 Output:
Language: C
Site: CodeToFun
Ready to learn C strings!
How It Works
%s in printf prints characters until it reaches '\0'. The compiler adds the null terminator automatically when you initialize with a string literal in an array.
Example 2 — char[] vs const char*
Understand writable arrays versus read-only literal pointers.
You may change bytes inside char editable[]. A const char* literal lives in read-only memory—copy into a char[] buffer before modifying. strcat appends but requires enough space in combined.
📈 Practical Patterns
Measure length, copy safely, compare, search, and split.
Example 3 — strlen() and Character Indexing
Measure string length and access individual characters by index.
C
#include <stdio.h>
#include <string.h>
int main(void) {
char word[] = "Hello";
printf("Length: %zu\n", strlen(word));
printf("First char: %c\n", word[0]);
printf("Last char: %c\n", word[strlen(word) - 1]);
for (size_t i = 0; i < strlen(word); i++) {
printf("word[%zu] = %c\n", i, word[i]);
}
return 0;
}
📤 Output:
Length: 5
First char: H
Last char: o
word[0] = H
word[1] = e
word[2] = l
word[3] = l
word[4] = o
How It Works
strlen counts bytes before '\0'—five letters for "Hello", not six. Valid indices are 0 through strlen(word) - 1. Index 5 is the null terminator.
Example 4 — strcpy() and strcmp()
Copy text into a buffer and test whether two strings match.
strcpy copies bytes including the terminating null. strcmp returns 0 when strings are equal, a negative value when the first string sorts before the second, and positive otherwise. Never use == to compare string contents—that compares pointers, not text.
Example 5 — strstr() and strtok()
Search for a substring and split a line into tokens—like PHP search and explode patterns.
Found substring at: banana,cherry
Token: apple
Token: banana
Token: cherry
How It Works
strstr returns a pointer into the haystack or NULL. strtok modifies line by writing '\0' over commas—call it only on writable buffers. After tokenizing, the original CSV layout is destroyed in memory.
Applications
🚀 Common Use Cases
User input — read names and commands with fgets into char buffers.
File paths — build and compare directory strings with strcat and strcmp.
Validation — check prefixes and suffixes with strstr and manual indexing.
CSV and config — split lines with strtok or span functions strspn/strcspn.
Sorting names — order text with strcmp or locale-aware strcoll.
Error messages — map errno codes to text with strerror.
🧠 How C Strings Work
1
You declare a char buffer
Create a char[] or copy input from scanf, fgets, or a file.
Create
2
Bytes end with \0
The null terminator tells library functions where text stops.
Terminate
3
string.h operates on bytes
Functions read or write characters until '\0' or a length limit.
Transform
=
💬
Output or next step
Print with printf, write to a file, or pass to another function.
Important
📝 Notes
C strings are null-terminated byte sequences, not abstract string objects.
Never use == to compare string content—use strcmp.
Do not pass string literals to strcpy, strcat, or strtok as the destination.
strcpy and strcat do not check destination size—prefer strncpy/strncat or snprintf when bounded.
strlen is O(n); cache the result if you need it repeatedly in a loop.
C string functions are global functions, not methods—unlike C# text.Length or PHP strlen($text) with different syntax rules.
Wrap Up
Conclusion
Strings are the backbone of C text handling. Master null-terminated char arrays, buffer sizing, and a handful of essential helpers like strlen(), strcpy(), strcmp(), strstr(), and strtok()—and you are ready to read real systems code.
Practice the examples on this page, then explore the full String Functions index for in-depth tutorials on all 29 functions in our series.
Use these points as you write your first C programs.
5
Core concepts
💬01
char + \0
Null-terminated.
Basics
📝02
Writable []
Not literals.
Safety
🔗03
string.h
Library helpers.
API
🛠04
strcmp
Not ==.
Compare
📚05
29 Functions
Full index.
Next step
❓ Frequently Asked Questions
A C string is a null-terminated array of char values—a sequence of characters ending with '\0'. You store text in char arrays like char name[50] = "Mari"; or point at string literals with const char*. The standard library in string.h provides functions to measure, copy, compare, and search strings.
char buffer[] is an array you can modify (writable memory). const char* points at read-only literal text or any char sequence. Never pass a string literal to functions that write into the string, such as strcpy or strtok—use a char array copy instead.
It marks the end of the string. Functions like strlen stop counting at the first '\0'. Forgetting to null-terminate after manual edits causes strlen and strcpy to read past your buffer—undefined behavior.
Use strcat(dest, src) to append when dest has enough space, or snprintf(dest, sizeof(dest), "%s %s", a, b) for safer formatting. strcat requires dest to already contain a valid null-terminated string.
Some do, some do not. strcpy, strcat, and strtok modify buffers. strlen, strcmp, strchr, and strstr only read. Always check each function's tutorial and never mutate string literals.
Learn char arrays and null termination on this page, then open the String Functions index for strlen(), strcpy(), strcmp(), strchr(), and strstr(). Those five functions cover length, copy, compare, and search—the core of C text handling.
Did you know?
C has no built-in string type like C# or Java. A “string” is just a convention: a char array with a null byte at the end. That is why you manage buffer sizes yourself—and why the C standard library offers dozens of helpers in <string.h> to copy, compare, and search text safely when used correctly.