String handling (string.h)¶
Include string.h for byte-string and raw memory operations.
Functions¶
| Function | Summary |
|---|---|
void * memchr( const void *, int, size_t ) |
Find the first matching byte within n bytes of an object. |
int memcmp( const void *, const void *, size_t ) |
Compare n bytes from two objects. |
void * memcpy( void *, const void *, size_t ) |
Copy n bytes from a non-overlapping source object to a destination object. |
void * memmove( void *, const void *, size_t ) |
Copy n bytes between objects, safely handling overlap. |
void * memset( void *, int, size_t ) |
Fill n bytes of an object with the low byte of c. |
char * strcat( char *, const char * ) |
Append one string to another. |
char * strchr( const char *, int ) |
Find the first occurrence of a character in a string. |
int strcmp( const char *, const char * ) |
Lexicographic string comparison. |
int strcoll( const char *, const char * ) |
Locale-independent string comparison, equivalent to strcmp in dcc. |
char * strcpy( char *, const char * ) |
Copy a string including its terminating NUL. |
size_t strcspn( const char *, const char * ) |
Length of the leading segment containing none of the reject characters. |
char * strdup( const char * ) |
Allocate and return a heap copy of a string. |
char * strerror( int ) |
Return text for an error number. |
int stricmp( const char *, const char * ) |
Case-insensitive lexicographic string comparison (ASCII only). |
size_t strlen( const char * ) |
Length of a string excluding the terminating NUL. |
char * strncat( char *, const char *, size_t ) |
Append at most n characters from one string to another, then write a NUL. |
int strncmp( const char *, const char *, size_t ) |
Compare at most n characters from two strings. |
char * strncpy( char *, const char *, size_t ) |
Copy at most n characters from one string to another, padding with NULs. |
char * strpbrk( const char *, const char * ) |
Find the first character from a set in a string. |
char * strrchr( const char *, int ) |
Find the last occurrence of a character in a string. |
size_t strspn( const char *, const char * ) |
Length of the leading segment containing only accepted characters. |
char * strstr( const char *, const char * ) |
Find the first occurrence of a substring in a string. |
char * strtok( char *, const char * ) |
Split a string into tokens separated by delimiter characters. |
size_t strxfrm( char *, const char *, size_t ) |
Transform s2 for strcoll comparison into s1, writing at most n chars; returns strlen(s2). |
Runtime model¶
The string and memory functions operate on byte strings and raw byte buffers.
In the fixed C locale, strcoll is equivalent to strcmp
and strxfrm uses the identity transform.
String and memory operations¶
String functions require NUL-terminated input and sufficient destination
storage. strlen excludes the NUL. Comparisons promise a negative, zero, or
positive result, not specifically -1, 0, or 1.
- Use
memmovewhen source and destination overlap;memcpyrequires non-overlapping objects. Memory functions operate on exact byte counts and do not stop at NUL. strcpyandstrcathave no capacity argument. Include space for the terminating NUL when allocating the destination.strncpy(dst, src, n)pads a short source with NULs, but does not terminate the result when the source has at leastncharacters.strncatlimits characters appended, not total destination capacity. It still writes a terminating NUL after them.strchr,strrchr,strstr, andmemchrreturn pointers into the input object orNULL; they do not allocate a copy.
#include <string.h>
char dst[16];
strcpy(dst, "hello");
if (strcmp(dst, "hello") == 0)
memset(dst, 0, sizeof dst);
Tokenizing a copy of a string with strtok writes NULs into the buffer. Pass
only modifiable strings; do not pass string literals:
#include <stdio.h>
#include <string.h>
char line[] = "alpha,beta,,gamma";
char *tok = strtok(line, ",");
while (tok) {
puts(tok); /* alpha, beta, gamma */
tok = strtok(NULL, ",");
}
strdup is the exception
Most string routines link nothing extra, but strdup allocates, so it
inherits the whole malloc chain. See the
appendix.
strdup and ASCII-only stricmp are extensions, not C89 functions. Check
strdup for NULL and release successful copies with free. strtok keeps
one internal parsing cursor, so interleaving tokenization of separate strings
overwrites the earlier scan state.