From Wikipedia, the free encyclopedia - View original article
|C standard library|
C string handling refers to a group of functions implementing operations on strings in the C standard library. Various operations, such as copying, concatenation, tokenization and searching are supported.
The only support for strings in the C programming language itself is that the compiler will translate a quoted string constant into a null-terminated string, which is stored in static memory. However, the C standard library provides a large number of functions designed to manipulate these null-terminated strings. These functions are so popular and used so often that they are usually considered part of the definition of C.
A string is a contiguous sequence of code units terminated by the first zero code (written
'\0' and corresponding to the ASCII null character). In C, there are two types of strings: string, which is sometimes called byte string which uses the type
chars as code units (one
char is at least 8 bits), and wide string which uses the type
wchar_t as code units.
A common misconception is that all
char arrays are strings, because string literals are converted to arrays during the compilation (or translation) phase. It is important to remember that a string ends at the first zero code unit. An array or string literal that contains a zero before the last byte therefore contains a string, or possibly several strings, but is not itself a string. Conversely, it is possible to create a
char array that is not null-terminated and is thus not a string:
char is often used as a small integer when needing to save memory.
The term pointer to a string is used in C to describe a pointer to the initial (lowest-addressed) byte of a string. In C, pointers are used to pass strings to functions. Documentation (including this page) will often use the term string to mean pointer to a string.
The term length of a string is used in C to describe the number of bytes preceding the zero byte.
strlen is a standardised function commonly used to determine the length of a string. A common mistake is to not realize that a string uses one more unit of memory than this length, in order to store the zero that ends the string.
Each string ends at the first occurrence of the zero code unit of the appropriate kind (
wchar_t). Consequently, a byte string can contain non-NUL characters in ASCII or any ASCII extension, but not characters in encodings such as UTF-16 (even though a 16-bit code unit might be nonzero, its high or low byte might be zero). The encodings that can be stored in wide strings are defined by the width of
wchar_t. In most implementations,
wchar_t is at least 16 bits, and so all 16-bit encodings, such as UCS-2, can be stored. If
wchar_t is 32-bits, then 32-bit encodings, such as UTF-32, can be stored.
Variable-width encodings can be used in both byte strings and wide strings. String length and offsets are measured in bytes or
wchar_t, not in "characters", which can be confusing to beginning programmers. UTF-8 and Shift JIS are often used in C byte strings, while UTF-16 is often used in C wide strings when
wchar_t is 16 bits. Truncating strings with variable length characters using functions like
strncpy can produce invalid sequences at the end of the string. This can be unsafe if the truncated parts are interpreted by code that assumes the input is valid.
Support for Unicode literals such as
char foo = "φωωβαρ";(UTF-8) or
wchar_t foo = L"φωωβαρ"; (UTF-16 or UTF-32) is implementation defined, and may require that the source code be in the same encoding. Some compilers or editors will require entering all non-ASCII characters as
\xNN sequences for each byte of UTF-8, and/or
\uNNNN for each word of UTF-16.
|This section's use of external links may not follow Wikipedia's policies or guidelines. (August 2012)|
Most of the functions that operate on C strings are defined in the
cstring header in C++). Functions that operate on C wide strings are defined in the
cwchar header in C++). These headers also contain declarations of functions used for handling memory buffers; 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, of which some popular ones are listed here. Some of these functions also violate const-correctness by accepting a
const string pointer and returning a non-
const pointer within the string. To correct this, some have been separated into two overloaded functions in the C++ version of the standard library.
In historical documentation the term "character" was often used instead of "byte" for C strings, which leads many to believe that these functions somehow do not work for UTF-8. In fact all lengths are defined as being in bytes and this is true in all implementations, and these functions work as well with UTF-8 as with any other byte encoding. The BSD documentation has been fixed to make this clear, but POSIX, Linux, and Windows documentation still uses "character" in many places where "byte" or "wchar_t" is the correct term.
|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.|
|type used for a code unit in a wide strings, usually either 16 or 32 bits.|
|integer type that can hold any value of a wchar_t as well as the value of the macro WEOF. This type is unchanged by integral promotions. Usually a 32 bit signed value.|
|contains all the information about the conversion state required from one call to a function to the other.|
|copies one string to another|
|writes exactly n bytes/|
|appends one string to another|
|appends no more than n bytes/|
|transforms a string according to the current locale|
|returns the length of the string|
|compares two strings|
|compares a specific number of bytes/|
|compares two strings according to the current locale|
|finds the first occurrence of a byte/|
|finds the last occurrence of a byte/|
|finds in a string the first occurrence of a byte/|
|finds in a string the last occurrence of a byte/|
|finds in a string the first occurrence of a byte/|
|finds the first occurrence of a substring in a string|
|splits string into tokens|
|Miscellaneous||N/A||returns a string containing a message derived from an error code|
|fills a buffer with a repeated byte/|
|copies one buffer to another|
|copies one buffer to another, possibly overlapping, buffer|
|compares two buffers|
|finds the first occurrence of a byte/|
|returns the number of bytes in the next multibyte character|
|converts the next multibyte character to a wide character|
|converts a wide character to its multibyte representation|
|converts a multibyte string to a wide string|
|converts a wide string to a multibyte string|
|convert a single-byte character to wide character, if possible|
|convert a wide character to a single-byte character, if possible|
|checks if a state object represents initial state|
|returns the number of bytes in the next multibyte character, given state|
|converts the next multibyte character to a wide character, given state|
|converts a wide character to its multibyte representation, given state|
|converts a multibyte string to a wide string, given state|
|converts a wide string to a multibyte string, given state|
"state" is used by encodings that rely on history such as shift states. This is not needed by UTF-8 or UTF-32. UTF-16 uses them to keep track of surrogate pairs and to hide the fact that it actually is a multi-word encoding.
The C standard library contains several functions for numeric conversions. The functions that deal with byte strings are defined in the
stdlib.h header (
cstdlib header in C++). The functions that deal with wide strings are defined in the
wchar.h header (
cwchar header in C++). Note that the
strtoxxx functions are not const-correct, since they accept a
const string pointer and return a non-
const pointer within the string.
|N/A||converts a string to a floating-point value|
|N/A||converts a string to an integer (C99)|
|converts a string to a floating-point value|
|converts a string to a signed integer|
|converts a string to an unsigned integer|
memccpy- SVID, POSIX - copies up to specified number of bytes between two memory areas, which must not overlap, stopping when a given byte is found.
mempcpy- GNU - a variant of
memcpyreturning a pointer to the byte following the last written byte
strcasecmp- POSIX, BSD - Case-insensitive versions of
strcat_s- C (2011) and ISO/IEC WDTR 24731 - a variant of
strcatthat checks the destination buffer size before copying
strcpy_s- C (2011) and ISO/IEC WDTR 24731 - a variant of
strcpythat checks the destination buffer size before copying
strdup- POSIX - allocates and duplicates a string
strerror_r- POSIX 1, GNU - a variant of
strerrorthat is thread-safe. GNU version is incompatible with POSIX one.
stricmp- Various - Case-insensitive versions of
strlcpy- BSD - a variant of
strcpythat truncates the result to fit in the destination buffer
strlcat- BSD - a variant of
strcatthat truncates the result to fit in the destination buffer
strsignal- POSIX:2008 - returns string representation of a signal code. Not thread safe.
strtok_r- POSIX - a variant of
strtokthat is thread-safe
Despite the well-established need to replace
strcpy with functions that do not overflow buffers, no accepted standard has arisen. Partly this is due to the mistaken belief by many C programmers that
strncpy have the desired behavior (neither function was designed for this and the behavior and arguments are non-intuitive and often written incorrectly even by expert programmers).
strcpy_s are defined in the C 11 (Annex K), and in ISO/IEC WDTR 24731. An error indicator is returned on buffer overflow and the output buffer is set to a zero-length string (which destroys data in the case of
strcat_s). These functions attracted considerable criticism because they are currently provided by default only by Microsoft Visual C++. Warning messages produced by Microsoft's compilers suggesting programmers use these functions instead of standard ones have been speculated by some to be a Microsoft attempt to lock developers to its platform. Open source code for these functions is available.
The more popular
strlcpy have been criticised on the basis that they encourage use of C strings and thus create more problems than they solve. Consequently they have not been included in the GNU C library (used by software on Linux), although they are implemented in OpenBSD, FreeBSD, NetBSD, Solaris, Mac OS X, QNX, and even internally in the Linux kernel. The lack of GNU C library support has not stopped various library authors from using it and bundling a replacement, among other SDL, glib2, ffmpeg, rsync... Open source code for these functions is available.
|The Wikibook C Programming has a page on the topic of: C Programming/Strings|