C Standard Library -
Introduction
string .h The header file defines one variable type, one macro, and various functions for manipulating character arrays.
is a header file in the C Standard Library that provides a set of functions for handling strings and memory blocks. These functions cover string copying, concatenation, comparison, searching, memory manipulation, and so on.
Library variables
The variable types defined in the header file string.h are listed below:
| No. | Variable & Description |
|---|---|
| 1 | **size_t ** |
| This is an unsigned integer type; it is the sizeof The result of the keyword. |
Library macros
The macros defined in the header file string.h are listed below:
| No. | Macro & description |
|---|---|
| 1 | NULL |
| This macro is the value of a null pointer constant. |
Library functions
The functions defined in the header file string.h are listed below:
| No. | Function & Description |
|---|---|
| 1 | void *memchr(const void *str, int c, size_t n) |
| Searches the first str Searches the first n bytes of the given string for the first occurrence of the character c (an unsigned char). | |
| 2 | int memcmp(const void *str1, const void *str2, size_t n) |
| Puts the str1 and str2 Compares the first n bytes of the two strings. | |
| 3 | void *memcpy(void *dest, const void *src, size_t n) |
| Copies n characters from src to dest. | |
| 4 | void *memmove(void *dest, const void *src, size_t n) |
| Another function used to copy n characters from src the source string to the destination string. dest . | |
| 5 | void *memset(void *str, int c, size_t n) |
| Copies the specified value c into the first n bytes of the memory area pointed to by str. | |
| 6 | char *strcat(char *dest, const char *src) |
| Puts the src appends the string pointed to by dest to the end of the string pointed to by . | |
| 7 | char *strncat(char *dest, const char *src, size_t n) |
| Puts the src appends the string pointed to by dest up to the end of the string pointed to by, up to a maximum of n characters. | |
| 8 | char *strchr(const char *str, int c) |
| Searches the first str Searches for the first occurrence of the character c (an unsigned char) in the given string. | |
| 9 | int strcmp(const char *str1, const char *str2) |
| Puts the str1 compares the string pointed to by str2 with the string pointed to by . | |
| 10 | int strncmp(const char *str1, const char *str2, size_t n) |
| Puts the str1 and str2 compares at most the first n bytes. | |
| 11 | int strcoll(const char *str1, const char *str2) |
| Puts the str1 and str2 for comparison, and the result depends on the locale setting of LC_COLLATE. | |
| 12 | char *strcpy(char *dest, const char *src) |
| Puts the src copies the string pointed to by dest. | |
| 13 | char *strncpy(char *dest, const char *src, size_t n) |
| Puts the src copies the string pointed to by dest, copying at most n characters. | |
| 14 | size_t strcspn(const char *str1, const char *str2) |
| Retrieves the number of consecutive characters at the beginning of string str1 that are not in string str2. | |
| 15 | char *strerror(int errnum) |
| Searches the internal array for the error number errnum and returns a pointer to an error message string. | |
| 16 | size_t strlen(const char *str) |
| Calculates the length of string str up to, but not including, the null-terminating character. | |
| 17 | char *strpbrk(const char *str1, const char *str2) |
| Search the string str1 for the first character that matches a character in the string str2 characters, not including the null terminator. That is, the characters in the string str1 are checked one by one; when the checked character is also contained in the string str2, the check stops and the position of that character is returned. | |
| 18 | char *strrchr(const char *str, int c) |
| Searches the first str Searches for the last occurrence of the character c (an unsigned char) in the given string. | |
| 19 | size_t strspn(const char *str1, const char *str2) |
| Search the string str1 for the first character that is not in the string str2 the index of the character that appears. | |
| 20 | char *strstr(const char *haystack, const char *needle) |
| in the string haystack for the first occurrence of the string needlethe position (not including the null-terminating character). | |
| 21 | char *strtok(char *str, const char *delim) |
| Split a string str into a set of strings,delim as the delimiter. | |
| 22 | size_t strxfrm(char *dest, const char *src, size_t n) |
| transforms the string according to LC_COLLATE in the program's current locale settings src 's first n characters and places them in the string dest it. |
Example
The following is how to use Examples of some functions in stdlib.h.
Copy a string:
Example
#include
#include
int
main
(
)
{
char
src
[
]
=
"Hello, World!"
;
char
dest
[
50
]
;
strcpy
(
dest
,
src
)
;
printf
(
"Copied string: %s\n"
,
dest
)
;
return
0
;
}
Concatenate strings:
Example
#include
#include
int
main
(
)
{
char
str1
[
50
]
=
"Hello"
;
char
str2
[
]
=
", World!"
;
strcat
(
str1
,
str2
)
;
printf
(
"Concatenated string: %s\n"
,
str1
)
;
return
0
;
}
Compare strings:
Example
#include
#include
int
main
(
)
{
char
str1
[
]
=
"Hello"
;
char
str2
[
]
=
"World"
;
int
result
=
strcmp
(
str1
,
str2
)
;
if
(
result
==
0
)
{
printf
(
"Strings are equal\n"
)
;
}
else
{
printf
(
"Strings are not equal\n"
)
;
}
return
0
;
}
Find the first occurrence of a character in a string:
Example
#include
#include
int
main
(
)
{
char
str
[
]
=
"Hello, World!"
;
char
ptr
=
strchr
(
str
,
'o'
)
;
if
(
ptr
!=
NULL
)
{
printf
(
"First occurrence of 'o' found at position: %ld\n"
,
ptr
str
)
;
}
else
{
printf
(
"Character not found\n"
)
;
}
return
0
;
}
Copy a memory block:
Example
#include
#include
int
main
(
)
{
char
src
[
]
=
"Hello, World!"
;
char
dest
[
50
]
;
memcpy
(
dest
,
src
,
strlen
(
src
)
1
)
;
printf
(
"Copied memory: %s\n"
,
dest
)
;
return
0
;
}
Notes
- When using string functions, ensure that the destination buffer has enough space to store the result, so as to avoid buffer overflow.
strncpyandstrncatSpecifying the maximum number of characters to copy or concatenate helps prevent buffer overflow.strtokThe function is not thread-safe because it uses a static variable internally to save context. If used in a multithreaded environment, you need to usestrtok_rfunction in Vue 3.- Use
memcpyWhen copying, the source and destination memory areas should not overlap. If they might overlap, you should usememmove.
By understanding and using The functions provided make string and memory operations convenient, allowing you to write more efficient and reliable C programs.