The C programming language has a set of functions implementing operations on strings (character strings and byte strings) in its standard library. Various operations, such as copying, concatenation, tokenization and searching are supported. For character strings, the standard library uses the convention that strings are null-terminated: a string of characters is represented as an array of elements, the last of which is a " character" with numeric value 0.
The only support for strings in the programming language proper is that the compiler translates quoted string constants into null-terminated strings.
Definitions A string is defined as a contiguous sequence of code units terminated by the first zero code unit (often called the NUL code unit). This means a string cannot contain the zero code unit, as the first one seen marks the end of the string. The length of a string is the number of code units before the zero code unit. The memory occupied by a string is always one more code unit than the length, as space is needed to store the zero terminator.
Generally, the term string means a string where the code unit is of type char, which is exactly 8 bits on all modern machines. C90 defines wide strings which use a code unit of type wchar_t, which is 16 or 32 bits on modern machines. This was intended for Unicode but it is increasingly common to use UTF-8 in normal strings for Unicode instead.
Strings are passed to functions by passing a pointer to the first code unit. Since char* and wchar_t* are different types, the functions that process wide strings are different than the ones processing normal strings and have different names.
String literals ("text" in the C source code) are converted to arrays (char[] in C, or const char[] in C++) during compilation. The result is an array of code units containing all the characters plus a trailing zero code unit. In C90 L"text" produces a wide string. A string literal can contain the zero code unit (one way is to put \0 into the source), but this will cause the string to end at that point. The rest of the literal will be placed in memory (with another zero code unit added to the end) but it is impossible to know those code units were translated from the string literal, therefore such source code is not a string literal.
Character encodings Each string ends at the first occurrence of the zero code unit of the appropriate kind (char or 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. (The standard requires a "type that holds any wide character", which on Windows no longer holds true since the UCS-2 to UTF-16 shift. This was recognized as a defect in the standard and fixed in C++.) C++11 and C11 add two types with explicit widths and .
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-width 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 (UTF-8) or (UTF-16 or UTF-32, depends on ) is implementation defined, and may require that the source code be in the same encoding, especially for where compilers might just copy whatever is between the quotes. 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. Since C11 (and C++11), a new literal prefix is available that guarantees UTF-8 for a bytestring literal, as in . Since C++20 and C23, a char8_t type was added that is meant to store UTF-8 characters and the types of u8 prefixed character and string literals were changed to char8_t and char8_t[] respectively.
Features
Terminology 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 single-byte encodings. 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.
Functions for handling memory buffers can process sequences of bytes that include null-byte as part of the data. Names of these functions typically start with mem, as opposite to the str prefix.
Headers Most of the functions that operate on C strings are declared in the string.h header (cstring in C++), while functions that operate on C wide strings are declared in the wchar.h header (cwchar 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 potential buffer overflows when not used carefully and properly, causing the programmers to prefer safer and possibly less portable variants, out of which some popular ones are listed below. 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.
Constants and types Name Notes 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 "wide" strings. The C standard only requires that be wide enough to hold the widest character set among the supported system locales and be greater or equal in size to . On Windows, the only platform to use extensively, it's defined as 16-bit which was enough to represent any Unicode (UCS-2) character, but is now only enough to represent a UTF-16 code unit, which can be half a code point. On other platforms it is defined as 32-bit and a Unicode code point always fits. This difference makes code using non-portable. Integer type that can hold any value of a wchar_t as well as the value of the macro WEOF. Usually a 32 bit signed value. Part of the C standard since C23, in , a type that is suitable for storing UTF-8 characters. Part of the C standard since C11, in , a type capable of holding 16 bits even if is another size. If the macro __STDC_UTF_16__ is defined as 1, the type is used for UTF-16 on that system. This is always the case in C23. C++ does not define such a macro, but the type is always used for UTF-16 in that language. Part of the C standard since C11, in , a type capable of holding 32 bits even if is another size. If the macro __STDC_UTF_32__ is defined as 1, the type is used for UTF-32 on that system. This is always the case in C23. C++ does not define such a macro, but the type is always used for UTF-32 in that language. Contains all the information about the conversion state required from one call to a function to the other.
Functions Bytestring Widestring DescriptionFor wide string functions substitute for "byte" in the description Stringmanipulation Copies one string to another Writes exactly bytes, copying from source or adding nulls Appends one string to another Appends no more than bytes from one string to another Transforms a string according to the current locale String examination Returns the length of the string Compares two strings (three-way comparison) Compares a specific number of bytes in two strings Compares two strings according to the current locale Finds the first occurrence of a byte in a string Finds the last occurrence of a byte in a string Returns the number of initial bytes in a string that are in a second string Returns the number of initial bytes in a string that are not in a second string Finds in a string the first occurrence of a byte in a set Finds the first occurrence of a substring in a string Splits a string into tokens Miscellaneous Returns a string containing a message derived from an error code Memorymanipulation Fills a buffer with a repeated byte. Since C23, was added to erase sensitive data. Copies one buffer to another. Since C23, was added to efficiently concatenate strings. Copies one buffer to another, possibly overlapping, buffer Compares two buffers (three-way comparison) Finds the first occurrence of a byte in a buffer
Multibyte functions Name Description 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 Converts a single-byte character to wide character, if possible Converts 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 Converts the next multibyte character to a UTF-8 character, given state Converts a single code point from UTF-8 to a narrow multibyte character representation, given state Converts the next multibyte character to a UTF-16 character, given state Converts a single code point from UTF-16 to a narrow multibyte character representation, given state Converts the next multibyte character to a UTF-32 character, given state Converts a single code point from UTF-32 to a narrow multibyte character representation, given state
These functions all need a object, originally in static memory (making the functions not be thread-safe) and in later additions the caller must maintain. This was originally intended to track shift states in the encodings, but modern ones such as UTF-8 do not need this. However these functions were designed on the assumption that the encoding is not a variable-width encoding and thus are designed to deal with exactly one at a time, passing it by value rather than using a string pointer. As UTF-16 is a variable-width encoding, the has been reused to keep track of surrogate pairs in the wide encoding, though the caller must still detect and call twice for a single character. Later additions to the standard admit that the only conversion programmers are interested in is between UTF-8 and UTF-16 and directly provide this.
Numeric conversions Bytestring Widestring DescriptionHere string refers either to byte string or wide string converts a string to a floating-point value ('atof' means 'ASCII to float') converts a string to an integer (C99) ('atoi' means 'ASCII to integer') (C99) (C99) (C99) (C99) converts a string to a floating-point value converts a string to a signed integer converts a string to an unsigned integer