String.h

From Wikipedia, the free encyclopedia

C++ Standard Library headers
C Standard Library headers
assert.h
complex.h
ctype.h
errno.h
fenv.h
float.h
inttypes.h
iso646.h
limits.h
locale.h
math.h
setjmp.h
signal.h
stdarg.h
stdbool.h
stddef.h
stdint.h
stdio.h
stdlib.h
string.h
tgmath.h
time.h
wchar.h
wctype.h
The correct title of this article is string.h. The initial letter is shown capitalized due to technical restrictions.

string.h is the header in the C standard library for the C programming language which contains macro definitions, constants, and declarations of functions and types used not only for string handling but also various memory handling functions; the name is thus something of a misnomer.

Functions declared in string.h are extremely popular, since as a part of the C standard library, they are guaranteed to work on any platform which supports C. However, some security issues exist with these functions, such as buffer overflows, leading programmers to prefer safer, possibly less portable variants. Also, the string functions only work with ASCII or character sets that extend ASCII in a compatible manner such as ISO-8859-1; multibyte ASCII-compatible character sets such as UTF-8 will work with the caveat that string "length" is to be interpreted as the count of bytes in the string rather than the count of Unicode characters. Non-ASCII compatible string handling is generally achieved through wchar.h.

Contents

[edit] Constants and types

Name Notes
NULL a macro expanding to the null pointer constant; that is, a constant representing a pointer value which is guaranteed not to be a valid address of an object in memory.
size_t an unsigned integer type which is the type of the result of the sizeof operator.

[edit] Functions

Name Notes
void *memcpy(void *dest, const void *src, size_t n); copies n bytes between two memory areas, which must not overlap
void *memmove(void *dest, const void *src, size_t n); copies n bytes between two memory areas; unlike with memcpy the areas may overlap
void *memchr(const void *s, int c, size_t n); returns a pointer to the first occurrence of c in the first n bytes of s, or NULL if not found
int memcmp(const void *s1, const void *s2, size_t n); compares the first n characters of two memory areas
void *memset(void *, int, size_t); overwrites a memory area with a byte pattern
char *strcat(char *dest, const char *src); appends the string src to dest
char *strncat(char *, const char *, size_t); appends at most n characters of the string src to dest
char *strchr(const char *, int); locates a character in a string, searching from the beginning
char *strrchr(const char *, int); locates a character in a string, searching from the end
int strcmp(const char *, const char *); compares two strings numerically
int strncmp(const char *, const char *, size_t); compares up to the first n bytes of two strings numerically
int strcoll(const char *, const char *); compares two strings using the current locale's collating order
char *strcpy(char *, const char *); copies a string from one location to another
char *strncpy(char *, const char *, size_t); copies up to n bytes of a string from one location to another
char *strerror(int); returns the string representation of errno (not thread-safe)
size_t strlen(const char *); finds the length of a C string
size_t strspn(const char *s, const char *accept); determines the length of the maximal initial substring of s consisting entirely of characters in accept
size_t strcspn(const char *s, const char *reject); determines the length of the maximal initial substring of s consisting entirely of characters not in reject
char *strpbrk(const char *s, const char *accept); finds the first occurrence of any character in accept in s
char *strstr(const char *haystack, const char *needle); finds the first occurrence of needle in haystack
char *strtok(char *, const char *); parses a string into a sequence of tokens; non-thread safe
size_t strxfrm(char *dest, const char *src, size_t n); transforms src into a collating form, such that the numerical sort order of the transformed string is equivalent to the collating order of src.

[edit] Extensions to ISO C

Name Notes Specification
char *strdup(const char *); allocates and duplicates a string into memory POSIX; originally a BSD extension
errno_t strcpy_s(char *restrict s1, rsize_t s1max, const char *restrict s2); bounds-checked variant of strcpy ISO/IEC WDTR 24731
void *mempcpy(void *dest, const void *src, size_t n); variant of memcpy returning a pointer to the byte following the last written byte GNU
void *memccpy(void *dest, const void *src, int c, size_t n

);

copies up to n bytes between two memory areas, which must not overlap, stopping when the byte c is found UNIX 98?
int *strerror_r(int, char *, size_t); returns the string representation of errno (thread-safe; some differences in semantics between GNU and XSI/POSIX) GNU, POSIX
size_t strlcpy(char *dest, const char *src, size_t n); bounds-checked variant of strcpy originally OpenBSD, now also FreeBSD, Solaris, OS X
char *strtok_r(char *, const char *, char **); thread-safe version of strtok POSIX

[edit] External links