All string functions in C are used to work with strings stored as character arrays. In C, a string is not a built-in object like in some modern languages. It is usually a sequence of characters ending with the null character '\0'. Because of this design, string operations such as finding length, copying, comparing, joining, and searching are done using library functions.
These functions are mostly available through the string.h header file. If you understand the important string functions properly, it becomes much easier to process names, text lines, tokens, user input, and file data. In this article, we will cover the major string functions in C, explain what they do, and show common examples with simple explanations.
What are String Functions in C?
String functions in C are predefined library functions used to perform operations on strings such as finding length, copying, concatenating, comparing, searching, and splitting text.
Most of them are declared in:
#include <string.h>String functions in C save time and reduce manual character-by-character processing.
Why String Functions are Important in C
- They make common text operations easier.
- They reduce repeated manual loop logic.
- They are heavily used in user input, parsing, and file processing.
- They are part of many interview and exam questions.
- They help build practical string-handling programs quickly.
Main String Functions in C
| Function | Use |
|---|---|
strlen() | Finds string length |
strcpy() | Copies one string into another |
strncpy() | Copies limited number of characters |
strcat() | Concatenates strings |
strncat() | Concatenates limited characters |
strcmp() | Compares two strings |
strncmp() | Compares limited characters |
strchr() | Finds first occurrence of a character |
strrchr() | Finds last occurrence of a character |
strstr() | Finds substring |
strtok() | Splits string into tokens |
strlen() in C
strlen() returns the number of characters in a string, excluding the null terminator.
#include <stdio.h>
#include <string.h>
int main(void)
{
char str[] = "Hello";
printf("Length = %zu\n", strlen(str));
return 0;
}Here the result is 5.
strcpy() and strncpy() in C
strcpy() copies one string into another destination array.
char source[] = "C Language";
char dest[20];
strcpy(dest, source);strncpy() copies only a specified number of characters.
char source[] = "Programming";
char dest[6];
strncpy(dest, source, 5);
dest[5] = '\0';Be careful with strncpy() because it may not add the null terminator if the limit is reached.
strcat() and strncat() in C
strcat() joins one string at the end of another.
char a[20] = "Hello ";
char b[] = "World";
strcat(a, b);strncat() appends only a limited number of characters.
char a[20] = "Hello ";
char b[] = "Programmers";
strncat(a, b, 4);Destination size must be large enough before using these functions.
strcmp() and strncmp() in C
strcmp() compares two strings lexicographically.
- returns
0if strings are equal - returns a negative value if first string is smaller
- returns a positive value if first string is greater
strcmp("abc", "abc");
strcmp("abc", "abd");
strcmp("cat", "bat");strncmp() compares only the first specified number of characters.
strchr() and strrchr() in C
strchr() finds the first occurrence of a character in a string, while strrchr() finds the last occurrence.
char str[] = "banana";
strchr(str, 'a');
strrchr(str, 'a');These functions return a pointer to the matched position, or NULL if the character is not found.
strstr() in C
strstr() searches for the first occurrence of a substring inside another string.
char text[] = "embedded systems";
strstr(text, "systems");If the substring exists, the function returns a pointer to its starting location.
strtok() in C
strtok() is used to split a string into tokens based on delimiters such as comma, space, or colon.
#include <stdio.h>
#include <string.h>
int main(void)
{
char str[] = "red,green,blue";
char *token = strtok(str, ",");
while (token != NULL)
{
printf("%s\n", token);
token = strtok(NULL, ",");
}
return 0;
}This function modifies the original string, so it should be used carefully.
Commonly Used Memory Functions Related to Strings in C
Some useful functions from related areas are not strictly string-only, but they are often used with strings.
| Function | Use |
|---|---|
memset() | Sets bytes to a value |
memcpy() | Copies a block of memory |
memmove() | Copies memory safely when overlap is possible |
memcmp() | Compares memory blocks |
These functions are byte-based and are useful in advanced string and buffer handling.
Common Mistakes with String Functions in C
- using
strcpy()without enough destination space - forgetting the null terminator after partial copy
- using
==instead ofstrcmp()for string comparison - modifying string literals directly
- using
strtok()without realizing it changes the original string
| Mistake | Problem | Better Practice |
|---|---|---|
if (a == b) | Compares addresses, not text | Use strcmp() |
| Small destination array | Overflow risk | Ensure enough space before copy or concatenation |
| Direct literal modification | Undefined behavior | Use writable character arrays |
Best Practices for String Functions in C
- Always make sure the destination array has enough space.
- Use bounded functions carefully and add null terminator when needed.
- Prefer clear and simple string logic instead of risky shortcuts.
- Understand whether a function returns a pointer, integer, or modified string.
- Test edge cases like empty strings and missing delimiters.
FAQs
Which header file is used for string functions in C?
The string.h header file is used for most standard string functions in C.
What does strlen() do in C?
strlen() returns the number of characters in a string, excluding the null terminator.
What is the difference between strcpy() and strncpy()?
strcpy() copies the entire string, while strncpy() copies only a specified number of characters.
Why should strcmp() be used instead of == for strings?
Because == compares pointer addresses, while strcmp() compares the actual text content.
What does strtok() do in C?
strtok() splits a string into tokens based on delimiters.
Can string functions cause overflow in C?
Yes. If the destination buffer is too small, functions like strcpy() and strcat() can cause overflow.