C string handling#strlen

{{other uses|C string (disambiguation)}}

{{Short description|Handling of strings in the C programming language}}

{{Use dmy dates|date=July 2020}}

{{C Standard Library}}

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 {{mvar|n}} characters is represented as an array of {{math|n + 1}} elements, the last of which is a "{{tt|NUL}} 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{{cite web |title=The C99 standard draft + TC3 |url=http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf |access-date=7 January 2011 |location=§7.1.1p1 }} 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 during compilation.{{cite web |title=The C99 standard draft + TC3 |url=http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf |access-date=7 January 2011 |location=§6.4.5p7 }} 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.{{cite web |title=The C99 standard draft + TC3 |url=http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf |access-date=7 January 2011 |location=Section 6.4.5 footnote 66}}

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 ({{code|char*}}) 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++.){{cite web|url=https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2022/p2460r2.pdf|title=Relax requirements on wchar_t to match existing practices}} C++11 and C11 add two types with explicit widths {{code|char16_t}} and {{code|char32_t}}.{{cite web |title=Fundamental types |url=https://en.cppreference.com/w/cpp/language/types |website=en.cppreference.com}}

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 {{Code|lang=c|1=char foo[512] = "φωωβαρ";}} (UTF-8) or {{Code|lang=c|1=wchar_t foo[512] = L"φωωβαρ";}} (UTF-16 or UTF-32, depends on {{code|wchar_t}}) is implementation defined,{{cite web |title=The C99 standard draft + TC3 |url=http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf |access-date=23 December 2011 |location=§5.1.1.2 Translation phases, p1 }} and may require that the source code be in the same encoding, especially for {{code|char}} 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 {{Code|lang=c|1=u8}} is available that guarantees UTF-8 for a bytestring literal, as in {{Code|lang=c|1=char foo[512] = u8"φωωβαρ";}}.{{cite web |title=string literals |url=https://en.cppreference.com/w/c/language/string_literal |website=en.cppreference.com |access-date=23 December 2019}} 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{{who|date=January 2017}} 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=

class="wikitable"
NameNotes
{{mono|NULL}}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.
{{anchor|wchar_t}}{{mono|wchar_t}}Type used for a code unit in "wide" strings. On Windows, the only platform to use {{mono|wchar_t}} extensively, it's defined as 16-bit{{Cite web |title=c++ - What is the use of wchar_t in general programming? |url=https://stackoverflow.com/questions/13509733/what-is-the-use-of-wchar-t-in-general-programming |access-date=2022-08-01 |website=Stack Overflow |language=en}} 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. The C standard only requires that {{mono|wchar_t}} be wide enough to hold the widest character set among the supported system locales{{cite web |url=https://pubs.opengroup.org/onlinepubs/007908775/xsh/stddef.h.html |title=stddef.h - standard type definitions |publisher=The Open Group |access-date=2017-01-28 }} and be greater or equal in size to {{mono|char}},{{cite book |title=Unicode Demystified: A Practical Programmer's Guide to the Encoding Standard |first=Richard |last=Gillam |url=https://books.google.com/books?id=wn5sXG8bEAcC&pg=PA714 |page=714 |publisher=Addison-Wesley Professional |year=2003|isbn=9780201700527 }}
{{anchor|wint_t}}{{mono|wint_t}}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.
{{anchor|char8_t}}{{mono|char8_t}}{{Cite web |title=char, wchar_t, char8_t, char16_t, char32_t |url=https://docs.microsoft.com/en-us/cpp/cpp/char-wchar-t-char16-t-char32-t |access-date=2022-08-01 |website=docs.microsoft.com |language=en-us}}Part of the C standard since C23, in {{mono|}}, a type that is suitable for storing UTF-8 characters.{{cite web|url=https://en.cppreference.com/w/c/string/multibyte/char8_t|title=char8_t}}
{{anchor|char16_t}}{{mono|char16_t}}{{Cite web|title =<cuchar> (uchar.h)|url=https://cplusplus.com/reference/cuchar/|language=en-us}}Part of the C standard since C11,{{cite web|url=https://en.cppreference.com/w/c/string/multibyte/char16_t|title=char16_t}} in {{mono|}}, a type capable of holding 16 bits even if {{mono|wchar_t}} 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.{{cite web|url=https://en.cppreference.com/w/c/preprocessor/replace#Predefined_macros|title=Replacing text macros}} C++ does not define such a macro, but the type is always used for UTF-16 in that language.{{cite web|url=https://en.cppreference.com/w/cpp/language/types#Character_types|title=Fundamental types}}
{{anchor|char32_t}}{{mono|char32_t}}Part of the C standard since C11,{{cite web|url=https://en.cppreference.com/w/c/string/multibyte/char32_t|title=char32_t}} in {{mono|}}, a type capable of holding 32 bits even if {{mono|wchar_t}} 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.
{{anchor|mbstate_t}}{{mono|mbstate_t}}Contains all the information about the conversion state required from one call to a function to the other.

=Functions=

class="wikitable"
! Byte
string

! Wide
string

! DescriptionFor wide string functions substitute {{mono|wchar_t}} for "byte" in the description

rowspan=5 | String
manipulation

| {{anchor|strcpy}}{{mono|strcpy}}{{cite web|url=https://en.cppreference.com/w/c/string/byte/strcpy |title=strcpy - cppreference.com |publisher=En.cppreference.com |date=2014-01-02 |access-date=2014-03-06}}

| {{anchor|wcscpy}}{{mono|wcscpy}}{{cite web|url=https://en.cppreference.com/w/c/string/wide/wcscpy |title=wcscpy - cppreference.com |publisher=En.cppreference.com |access-date=2014-03-06}}

| Copies one string to another

{{anchor|strncpy}}{{mono|strncpy}}{{cite web|url=https://en.cppreference.com/w/c/string/byte/strncpy |title=strncpy - cppreference.com |publisher=En.cppreference.com |date=2013-10-04 |access-date=2014-03-06}}

| {{anchor|wcsncpy}}{{mono|wcsncpy}}{{cite web|url=https://en.cppreference.com/w/c/string/wide/wcsncpy |title=wcsncpy - cppreference.com |publisher=En.cppreference.com |access-date=2014-03-06}}

| Writes exactly {{mvar|n}} bytes, copying from source or adding nulls

{{anchor|strcat}}{{mono|strcat}}{{cite web|url=https://en.cppreference.com/w/c/string/byte/strcat |title=strcat - cppreference.com |publisher=En.cppreference.com |date=2013-10-08 |access-date=2014-03-06}}

| {{anchor|wcscat}}{{mono|wcscat}}{{cite web|url=https://en.cppreference.com/w/c/string/wide/wcscat |title=wcscat - cppreference.com |publisher=En.cppreference.com |access-date=2014-03-06}}

| Appends one string to another

{{anchor|strncat}}{{mono|strncat}}{{cite web|url=https://en.cppreference.com/w/c/string/byte/strncat |title=strncat - cppreference.com |publisher=En.cppreference.com |date=2013-07-01 |access-date=2014-03-06}}

| {{anchor|wcsncat}}{{mono|wcsncat}}{{cite web|url=https://en.cppreference.com/w/c/string/wide/wcsncat |title=wcsncat - cppreference.com |publisher=En.cppreference.com |access-date=2014-03-06}}

| Appends no more than {{mvar|n}} bytes from one string to another

{{anchor|strxfrm}}{{mono|strxfrm}}{{cite web|url=https://en.cppreference.com/w/c/string/byte/strxfrm |title=strxfrm - cppreference.com |publisher=En.cppreference.com |access-date=2014-03-06}}

| {{anchor|wcsxfrm}}{{mono|wcsxfrm}}{{cite web|url=https://en.cppreference.com/w/c/string/wide/wcsxfrm |title=wcsxfrm - cppreference.com |publisher=En.cppreference.com |access-date=2014-03-06}}

| Transforms a string according to the current locale

rowspan=11 | String
examination

| {{anchor|strlen}}{{mono|strlen}}{{cite web|url=https://en.cppreference.com/w/c/string/byte/strlen |title=strlen - cppreference.com |publisher=En.cppreference.com |date=2013-12-27 |access-date=2014-03-06}}

| {{anchor|wcslen}}{{mono|wcslen}}{{cite web|url=https://en.cppreference.com/w/c/string/wide/wcslen |title=wcslen - cppreference.com |publisher=En.cppreference.com |access-date=2014-03-06}}

| Returns the length of the string

{{anchor|strcmp}}{{mono|strcmp}}{{cite web|url=https://en.cppreference.com/w/c/string/byte/strcmp |title=strcmp - cppreference.com |publisher=En.cppreference.com |access-date=2014-03-06}}

| {{anchor|wcscmp}}{{mono|wcscmp}}{{cite web|url=https://en.cppreference.com/w/c/string/wide/wcscmp |title=wcscmp - cppreference.com |publisher=En.cppreference.com |access-date=2014-03-06}}

| Compares two strings (three-way comparison)

{{anchor|strncmp}}{{mono|strncmp}}{{cite web|url=https://en.cppreference.com/w/c/string/byte/strncmp |title=strncmp - cppreference.com |publisher=En.cppreference.com |access-date=2014-03-06}}

| {{anchor|wcsncmp}}{{mono|wcsncmp}}{{cite web|url=https://en.cppreference.com/w/c/string/wide/wcsncmp |title=wcsncmp - cppreference.com |publisher=En.cppreference.com |access-date=2014-03-06}}

| Compares a specific number of bytes in two strings

{{anchor|strcoll}}{{mono|strcoll}}{{cite web|url=https://en.cppreference.com/w/c/string/byte/strcoll |title=strcoll - cppreference.com |publisher=En.cppreference.com |access-date=2014-03-06}}

| {{anchor|wcscoll}}{{mono|wcscoll}}{{cite web|url=https://en.cppreference.com/w/c/string/wide/wcscoll |title=wcscoll - cppreference.com |publisher=En.cppreference.com |access-date=2014-03-06}}

| Compares two strings according to the current locale

{{anchor|strchr}}{{mono|strchr}}{{cite web|url=https://en.cppreference.com/w/c/string/byte/strchr |title=strchr - cppreference.com |publisher=En.cppreference.com |date=2014-02-23 |access-date=2014-03-06}}

| {{anchor|wcschr}}{{mono|wcschr}}{{cite web|url=https://en.cppreference.com/w/c/string/wide/wcschr |title=wcschr - cppreference.com |publisher=En.cppreference.com |access-date=2014-03-06}}

| Finds the first occurrence of a byte in a string

{{anchor|strrchr}}{{mono|strrchr}}{{cite web|url=https://en.cppreference.com/w/c/string/byte/strrchr |title=strrchr - cppreference.com |publisher=En.cppreference.com |access-date=2014-03-06}}

| {{anchor|wcsrchr}}{{mono|wcsrchr}}{{cite web|url=https://en.cppreference.com/w/c/string/wide/wcsrchr |title=wcsrchr - cppreference.com |publisher=En.cppreference.com |access-date=2014-03-06}}

| Finds the last occurrence of a byte in a string

{{anchor|strspn}}{{mono|strspn}}{{cite web|url=https://en.cppreference.com/w/c/string/byte/strspn |title=strspn - cppreference.com |publisher=En.cppreference.com |access-date=2014-03-06}}

| {{anchor|wcsspn}}{{mono|wcsspn}}{{cite web|url=https://en.cppreference.com/w/c/string/wide/wcsspn |title=wcsspn - cppreference.com |publisher=En.cppreference.com |access-date=2014-03-06}}

| Returns the number of initial bytes in a string that are in a second string

{{anchor|strcspn}}{{mono|strcspn}}{{cite web|url=https://en.cppreference.com/w/c/string/byte/strcspn |title=strcspn - cppreference.com |publisher=En.cppreference.com |date=2013-05-31 |access-date=2014-03-06}}

| {{anchor|wcscspn}}{{mono|wcscspn}}{{cite web|url=https://en.cppreference.com/w/c/string/wide/wcscspn |title=wcscspn - cppreference.com |publisher=En.cppreference.com |access-date=2014-03-06}}

| Returns the number of initial bytes in a string that are not in a second string

{{anchor|strpbrk}}{{mono|strpbrk}}{{cite web|url=https://en.cppreference.com/w/c/string/byte/strpbrk |title=strpbrk - cppreference.com |publisher=En.cppreference.com |date=2013-05-31 |access-date=2014-03-06}}

| {{anchor|wcspbrk}}{{mono|wcspbrk}}{{cite web|url=https://en.cppreference.com/w/c/string/wide/wcspbrk |title=wcspbrk - cppreference.com |publisher=En.cppreference.com |access-date=2014-03-06}}

| Finds in a string the first occurrence of a byte in a set

{{anchor|strstr}}{{mono|strstr}}{{cite web|url=https://en.cppreference.com/w/c/string/byte/strstr |title=strstr - cppreference.com |publisher=En.cppreference.com |date=2013-10-16 |access-date=2014-03-06}}

| {{anchor|wcsstr}}{{mono|wcsstr}}{{cite web|url=https://en.cppreference.com/w/c/string/wide/wcsstr |title=wcsstr - cppreference.com |publisher=En.cppreference.com |access-date=2014-03-06}}

| Finds the first occurrence of a substring in a string

{{anchor|strtok}}{{mono|strtok}}{{cite web|url=https://en.cppreference.com/w/c/string/byte/strtok |title=strtok - cppreference.com |publisher=En.cppreference.com |date=2013-09-03 |access-date=2014-03-06}}

| {{anchor|wcstok}}{{mono|wcstok}}{{cite web|url=https://en.cppreference.com/w/c/string/wide/wcstok |title=wcstok - cppreference.com |publisher=En.cppreference.com |access-date=2014-03-06}}

| Splits a string into tokens

Miscellaneous

| {{anchor|strerror}}{{mono|strerror}}{{cite web|url=https://en.cppreference.com/w/c/string/byte/strerror |title=strerror - cppreference.com |publisher=En.cppreference.com |date=2013-05-31 |access-date=2014-03-06}}

| {{n/a}}

| Returns a string containing a message derived from an error code

rowspan=5 | Memory
manipulation

|{{anchor|memset}}{{mono|memset}}{{cite web|url=https://en.cppreference.com/w/c/string/byte/memset |title=memset - cppreference.com |publisher=En.cppreference.com |access-date=2014-03-06}}

|{{anchor|wmemset}}{{mono|wmemset}}{{cite web|url=https://en.cppreference.com/w/c/string/wide/wmemset |title=wmemset - cppreference.com |publisher=En.cppreference.com |access-date=2014-03-06}}

| Fills a buffer with a repeated byte. Since C23, {{mono|memset_explicit()}} was added to erase sensitive data.

{{anchor|memcpy}}{{mono|memcpy}}{{cite web|url=https://en.cppreference.com/w/c/string/byte/memcpy |title=memcpy - cppreference.com |publisher=En.cppreference.com |access-date=2014-03-06}}

|{{anchor|wmemcpy}}{{mono|wmemcpy}}{{cite web|url=https://en.cppreference.com/w/c/string/wide/wmemcpy |title=wmemcpy - cppreference.com |publisher=En.cppreference.com |access-date=2014-03-06}}

| Copies one buffer to another. Since C23, {{mono|memccpy()}} was added to efficiently concatenate strings.

{{anchor|memmove}}{{mono|memmove}}{{cite web|url=https://en.cppreference.com/w/c/string/byte/memmove |title=memmove - cppreference.com |publisher=En.cppreference.com |date=2014-01-25 |access-date=2014-03-06}}

|{{anchor|wmemmove}}{{mono|wmemmove}}{{cite web|url=https://en.cppreference.com/w/c/string/wide/wmemmove |title=wmemmove - cppreference.com |publisher=En.cppreference.com |access-date=2014-03-06}}

| Copies one buffer to another, possibly overlapping, buffer

{{anchor|memcmp}}{{mono|memcmp}}{{cite web|url=https://en.cppreference.com/w/c/string/byte/memcmp |title=memcmp - cppreference.com |publisher=En.cppreference.com |access-date=2014-03-06}}

|{{anchor|wmemcmp}}{{mono|wmemcmp}}{{cite web|url=https://en.cppreference.com/w/c/string/wide/wmemcmp |title=wmemcmp - cppreference.com |publisher=En.cppreference.com |access-date=2014-03-06}}

| Compares two buffers (three-way comparison)

{{anchor|memchr}}{{mono|memchr}}{{cite web|url=https://en.cppreference.com/w/c/string/byte/memchr |title=memchr - cppreference.com |publisher=En.cppreference.com |access-date=2014-03-06}}

|{{anchor|wmemchr}}{{mono|wmemchr}}{{cite web|url=https://en.cppreference.com/w/c/string/wide/wmemchr |title=wmemchr - cppreference.com |publisher=En.cppreference.com |access-date=2014-03-06}}

| Finds the first occurrence of a byte in a buffer

colspan=4 |

==Multibyte functions==

class="wikitable" style="margin-right: 1.5em;"
Name

! Description

{{anchor|mblen}}{{mono|mblen}}{{cite web|url=https://en.cppreference.com/w/c/string/multibyte/mblen |title=mblen - cppreference.com |publisher=En.cppreference.com |access-date=2014-03-06}}

| Returns the number of bytes in the next multibyte character

{{anchor|mbtowc}}{{mono|mbtowc}}{{cite web|url=https://en.cppreference.com/w/c/string/multibyte/mbtowc |title=mbtowc - cppreference.com |publisher=En.cppreference.com |access-date=2014-03-06}}

| Converts the next multibyte character to a wide character

{{anchor|wctomb}}{{mono|wctomb}}{{cite web|url=https://en.cppreference.com/w/c/string/multibyte/wctomb |title=wctomb - cppreference.com |publisher=En.cppreference.com |date=2014-02-04 |access-date=2014-03-06}}

| Converts a wide character to its multibyte representation

{{anchor|mbstowcs}}{{mono|mbstowcs}}{{cite web|url=https://en.cppreference.com/w/c/string/multibyte/mbstowcs |title=mbstowcs - cppreference.com |publisher=En.cppreference.com |access-date=2014-03-06}}

| Converts a multibyte string to a wide string

{{anchor|wcstombs}}{{mono|wcstombs}}{{cite web|url=https://en.cppreference.com/w/c/string/multibyte/wcstombs |title=wcstombs - cppreference.com |publisher=En.cppreference.com |access-date=2014-03-06}}

| Converts a wide string to a multibyte string

{{anchor|btowc}}{{mono|btowc}}{{cite web|url=https://en.cppreference.com/w/c/string/multibyte/btowc |title=btowc - cppreference.com |publisher=En.cppreference.com |access-date=2014-03-06}}

| Converts a single-byte character to wide character, if possible

{{anchor|wctob}}{{mono|wctob}}{{cite web|url=https://en.cppreference.com/w/c/string/multibyte/wctob |title=wctob - cppreference.com |publisher=En.cppreference.com |access-date=2014-03-06}}

| Converts a wide character to a single-byte character, if possible

{{anchor|mbsinit}}{{mono|mbsinit}}{{cite web|url=https://en.cppreference.com/w/c/string/multibyte/mbsinit |title=mbsinit - cppreference.com |publisher=En.cppreference.com |access-date=2014-03-06}}

| Checks if a state object represents initial state

{{anchor|mbrlen}}{{mono|mbrlen}}{{cite web|url=https://en.cppreference.com/w/c/string/multibyte/mbrlen |title=mbrlen - cppreference.com |publisher=En.cppreference.com |access-date=2014-03-06}}

| Returns the number of bytes in the next multibyte character, given state

{{anchor|mbrtowc}}{{mono|mbrtowc}}{{cite web|url=https://en.cppreference.com/w/c/string/multibyte/mbrtowc |title=mbrtowc - cppreference.com |publisher=En.cppreference.com |access-date=2014-03-06}}

| Converts the next multibyte character to a wide character, given state

{{anchor|wcrtomb}}{{mono|wcrtomb}}{{cite web|url=https://en.cppreference.com/w/c/string/multibyte/wcrtomb |title=wcrtomb - cppreference.com |publisher=En.cppreference.com |access-date=2014-03-06}}

| Converts a wide character to its multibyte representation, given state

{{anchor|mbsrtowcs}}{{mono|mbsrtowcs}}{{cite web|url=https://en.cppreference.com/w/c/string/multibyte/mbsrtowcs |title=mbsrtowcs - cppreference.com |publisher=En.cppreference.com |access-date=2014-03-06}}

| Converts a multibyte string to a wide string, given state

{{anchor|wcsrtombs}}{{mono|wcsrtombs}}{{cite web|url=https://en.cppreference.com/w/c/string/multibyte/wcsrtombs |title=wcsrtombs - cppreference.com |publisher=En.cppreference.com |access-date=2014-03-06}}

| Converts a wide string to a multibyte string, given state

{{anchor|mbrtoc8}}{{mono|mbrtoc8}}{{cite web|url=https://en.cppreference.com/w/c/string/multibyte/mbrtoc8 |title=mbrtoc8 - cppreference.com |publisher=En.cppreference.com}}

| Converts the next multibyte character to a UTF-8 character, given state

{{anchor|c8rtomb}}{{mono|c8rtomb}}{{cite web|url=https://en.cppreference.com/w/c/string/multibyte/c8rtomb|title=c8rtomb - cppreference.com |publisher=En.cppreference.com}}

| Converts a single code point from UTF-8 to a narrow multibyte character representation, given state

{{anchor|mbrtoc16}}{{mono|mbrtoc16}}{{cite web|url=https://en.cppreference.com/w/c/string/multibyte/mbrtoc16 |title=mbrtoc16 - cppreference.com |publisher=En.cppreference.com}}

| Converts the next multibyte character to a UTF-16 character, given state

{{anchor|c16rtomb}}{{mono|c16rtomb}}{{cite web|url=https://en.cppreference.com/w/c/string/multibyte/c16rtomb|title=c16rtomb - cppreference.com |publisher=En.cppreference.com}}

| Converts a single code point from UTF-16 to a narrow multibyte character representation, given state

{{anchor|mbrtoc32}}{{mono|mbrtoc32}}{{cite web|url=https://en.cppreference.com/w/c/string/multibyte/mbrtoc32 |title=mbrtoc32 - cppreference.com |publisher=En.cppreference.com}}

| Converts the next multibyte character to a UTF-32 character, given state

{{anchor|c32rtomb}}{{mono|c32rtomb}}{{cite web|url=https://en.cppreference.com/w/c/string/multibyte/c32rtomb|title=c23rtomb - cppreference.com |publisher=En.cppreference.com}}

| Converts a single code point from UTF-32 to a narrow multibyte character representation, given state

These functions all need a {{samp|mbstate_t}} 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 {{samp|mb}} encodings, but modern ones such as UTF-8 do not need this. However these functions were designed on the assumption that the {{samp|wc}} encoding is not a variable-width encoding and thus are designed to deal with exactly one {{samp|wchar_t}} at a time, passing it by value rather than using a string pointer. As UTF-16 is a variable-width encoding, the {{samp|mbstate_t}} has been reused to keep track of surrogate pairs in the wide encoding, though the caller must still detect and call {{samp|mbtowc}} twice for a single character.{{cite web |url=https://www.gnu.org/software/libc/manual/html_node/Keeping-the-state.html |title=6.3.2 Representing the state of the conversion |access-date=2017-01-31 | website=The GNU C Library}}{{cite web |url=https://git.musl-libc.org/cgit/musl/tree/src/multibyte/c16rtomb.c |title=root/src/multibyte/c16rtomb.c |access-date=2017-01-31 }}{{cite web |url=https://svnweb.freebsd.org/base/stable/11/lib/libc/locale/c16rtomb.c?view=markup |title=Contents of /stable/11/lib/libc/locale/c16rtomb.c |access-date=2017-01-31 }} 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.

{{Clear}}

={{anchor|stdlib.h}}Numeric conversions=

class="wikitable" style="margin-right: 1.5em;"
Byte
string

! Wide
string

! DescriptionHere string refers either to byte string or wide string

{{anchor|atof}}{{mono|atof}}{{cite web|url=https://en.cppreference.com/w/c/string/byte/atof |title=atof - cppreference.com |publisher=En.cppreference.com |date=2013-05-31 |access-date=2014-03-06}}

| {{n/a}}

| converts a string to a floating-point value ('atof' means 'ASCII to float')

{{anchor|atoi|atol|atoll}}{{mono|atoi}}
{{mono|atol}}
{{mono|atoll}}{{cite web|url=https://en.cppreference.com/w/c/string/byte/atoi |title=atoi, atol, atoll - cppreference.com |publisher=En.cppreference.com |date=2014-01-18 |access-date=2014-03-06}}

| {{n/a}}

| converts a string to an integer (C99) ('atoi' means 'ASCII to integer')

{{anchor|strtof|strtod|strtold}}{{mono|strtof}} (C99){{cite web|url=https://en.cppreference.com/w/c/string/byte/strtof |title=strtof, strtod, strtold - cppreference.com |publisher=En.cppreference.com |date=2014-02-04 |access-date=2014-03-06}}
{{mono|strtod}}{{cite web|url=https://en.cppreference.com/w/c/string/byte/strtod |title=strtof, strtod, strtold - cppreference.com |publisher=En.cppreference.com |date=2014-02-04 |access-date=2014-03-06}}
{{mono|strtold}} (C99){{cite web|url=https://en.cppreference.com/w/c/string/byte/strtold |title=strtof, strtod, strtold - cppreference.com |publisher=En.cppreference.com |date=2014-02-04 |access-date=2014-03-06}}

| {{anchor|wcstof|wcstod|wcstold}}{{mono|wcstof}} (C99){{cite web|url=https://en.cppreference.com/w/c/string/wide/wcstof |title=wcstof, wcstod, wcstold - cppreference.com |publisher=En.cppreference.com |access-date=2014-03-06}}
{{mono|wcstod}}{{cite web|url=https://en.cppreference.com/w/c/string/wide/wcstod |title=wcstof, wcstod, wcstold - cppreference.com |publisher=En.cppreference.com |access-date=2014-03-06}}
{{mono|wcstold}} (C99){{cite web|url=http://en.cppreference.com/w/c/string/wide/wcstold |title=wcstof, wcstod, wcstold - cppreference.com |publisher=En.cppreference.com |access-date=2014-03-06}}

| converts a string to a floating-point value

{{anchor|strtol|strtoll}}{{mono|strtol}}
{{mono|strtoll}}{{cite web|url=https://en.cppreference.com/w/c/string/byte/strtol |title=strtol, strtoll - cppreference.com |publisher=En.cppreference.com |date=2014-02-04 |access-date=2014-03-06}}

| {{anchor|wcstol|wcstoll}}{{mono|wcstol}}
{{mono|wcstoll}}{{cite web|url=https://en.cppreference.com/w/c/string/wide/wcstol |title=wcstol, wcstoll - cppreference.com |publisher=En.cppreference.com |access-date=2014-03-06}}

| converts a string to a signed integer

{{anchor|strtoul|strtoull}}{{mono|strtoul}}
{{mono|strtoull}}{{cite web|url=https://en.cppreference.com/w/c/string/byte/strtoul |title=strtoul, strtoull - cppreference.com |publisher=En.cppreference.com |date=2014-02-04 |access-date=2014-03-06}}

| {{anchor|wcstoul|wcstoull}}{{mono|wcstoul}}
{{mono|wcstoull}}{{cite web|url=https://en.cppreference.com/w/c/string/wide/wcstoul |title=wcstoul, wcstoull - cppreference.com |publisher=En.cppreference.com |access-date=2014-03-06}}

| converts a string to an unsigned integer

colspan=3 |

The C standard library contains several functions for numeric conversions. The functions that deal with byte strings are defined in the {{code|stdlib.h}} header ({{code|cstdlib}} header in C++). The functions that deal with wide strings are defined in the {{code|wchar.h}} header ({{code|cwchar}} header in C++).

The functions {{code|strchr}}, {{code|bsearch}}, {{code|strpbrk}}, {{code|strrchr}}, {{code|strstr}}, {{code|memchr}} and their wide counterparts are not const-correct, since they accept a {{code|const}} string pointer and return a non-{{code|const}} pointer within the string. This has been fixed in C23.{{cite web |title=WG14-N3020 : Qualifier-preserving standard library functions, v4 |url=https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3020.pdf |website=open-std.org |date=2022-06-13}}

Also, since the Normative Amendment 1 (C95), {{code|atoxx}} functions are considered subsumed by {{code|strtoxxx}} functions, for which reason neither C95 nor any later standard provides wide-character versions of these functions. The argument against {{code|atoxx}} is that they do not differentiate between an error and a {{code|0}}.[http://www.open-std.org/jtc1/sc22/wg14/www/C99RationaleV5.10.pdf C99 Rationale, 7.20.1.1]{{clear}}

Popular extensions

class="wikitable"
NameSourceDescription
{{anchor|bzero}}{{mono|bzero}}{{cite web|url=http://pubs.opengroup.org/onlinepubs/009695399/functions/bzero.html |title=bzero |publisher=The Open Group |access-date=2017-11-27}}{{cite web|url=https://man.openbsd.org/explicit_bzero.3 |title=bzero(3) |publisher=OpenBSD |access-date=2017-11-27}}BSDFills a buffer with zero bytes, deprecated by {{mono|memset}}
{{anchor|memccpy}}{{mono|memccpy}}{{cite web|url=http://pubs.opengroup.org/onlinepubs/009695399/functions/memccpy.html |title=memccpy |publisher=Pubs.opengroup.org |access-date=2014-03-06}}SVIDPart of the C standard since C23, copies between two non-overlapping memory areas, stopping when a given byte is found.
{{anchor|mempcpy}}{{mono|mempcpy}}{{cite web|url=https://www.kernel.org/doc/man-pages/online/pages/man3/mempcpy.3.html |title=mempcpy(3) - Linux manual page |publisher=Kernel.org |access-date=2014-03-06}}GNUa variant of #memcpy returning a pointer to the byte following the last written byte
{{anchor|strcasecmp}}{{mono|strcasecmp}}{{cite web|url=https://www.kernel.org/doc/man-pages/online/pages/man3/strcasecmp.3.html |title=strcasecmp(3) - Linux manual page |publisher=Kernel.org |access-date=2014-03-06}}BSDcase-insensitive version of {{mono|strcmp}}
{{anchor|strcat_s}}{{mono|strcat_s}}{{cite web|url=https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/strcat-s-wcscat-s-mbscat-s?view=msvc-170 |title=strcat_s, wcscat_s, _mbscat_s |publisher=docs.microsoft.com |access-date=2022-04-22}}Windowsa variant of #strcat that checks the destination buffer size before copying
{{anchor|strcpy_s}}{{mono|strcpy_s}}{{cite web|url=https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/strcpy-s-wcscpy-s-mbscpy-s?view=msvc-170 |title=strcpy_s, wcscpy_s, _mbscpy_s, _mbscpy_s_l |publisher=docs.microsoft.com |access-date=2022-04-22}}Windowsa variant of #strcpy that checks the destination buffer size before copying
{{anchor|strdup}}{{mono|strdup}} & {{mono|strndup}}{{cite web|url=http://pubs.opengroup.org/onlinepubs/009695399/functions/strdup.html |title=strdup |publisher=Pubs.opengroup.org |access-date=2014-03-06}}POSIXPart of the C standard since C23, allocates and duplicates a string
{{anchor|strerror_r}}{{mono|strerror_r}}{{cite web|url=http://man7.org/linux/man-pages/man3/strerror.3.html |title=strerror(3) - Linux manual page |publisher=man7.org |access-date=2019-11-03}}POSIX 1, GNUa variant of #strerror that is thread-safe. The GNU version is incompatible with the POSIX one.
{{anchor|stricmp}}{{mono|stricmp}}{{cite web|url=http://www.cprogrammingexpert.com/C/Tutorial/strings/stricmp.aspx |title=String {{pipe}} stricmp() |publisher=C Programming Expert.com |access-date=2014-03-06}}Windowscase-insensitive version of {{mono|strcmp}}
{{anchor|strlcpy}}{{mono|strlcpy}}{{cite web|url=https://man.openbsd.org/OpenBSD-5.9/man3/strlcat.3 |title= strlcpy, strlcat — size-bounded string copying and concatenation |publisher=OpenBSD |access-date=2016-05-26}}BSDa variant of #strcpy that truncates the result to fit in the destination buffer{{cite web | url=https://www.millert.dev/papers/strlcpy | title=strlcpy and strlcat – consistent, safe, string copy and concatenation. | author=Todd C. Miller |author2=Theo de Raadt | year=1999 | publisher=USENIX '99 }}
{{anchor|strlcat}}{{mono|strlcat}}BSDa variant of #strcat that truncates the result to fit in the destination buffer
{{anchor|strsignal}}{{mono|strsignal}}{{cite web|url=https://pubs.opengroup.org/onlinepubs/9699919799/functions/strsignal.html |title=strsignal |publisher=Pubs.opengroup.org |access-date=2014-03-06}}POSIX:2008returns string representation of a signal code. Not thread safe.
{{anchor|strtok_r}}{{mono|strtok_r}}{{cite web|url=https://pubs.opengroup.org/onlinepubs/009695399/functions/strtok.html |title=strtok |publisher=Pubs.opengroup.org |access-date=2014-03-06}}POSIXa variant of #strtok that is thread-safe

Replacements

Despite the well-established need to replace strcat and strcpy with functions that do not allow buffer overflows, no accepted standard has arisen. This is partly due to the mistaken belief by many C programmers that strncat and strncpy have the desired behavior; however, neither function was designed for this (they were intended to manipulate null-padded fixed-size string buffers, a data format less commonly used in modern software), and the behavior and arguments are non-intuitive and often written incorrectly even by expert programmers.

The most popular{{Efn|On GitHub, there are 7,813,206 uses of strlcpy, versus 38,644 uses of strcpy_s (and 15,286,150 uses of strcpy).{{Citation needed|date=February 2015}}}} replacement are the strlcat{{cite web|url=http://bxr.su/OpenBSD/lib/libc/string/strlcpy.c |title= strlcpy.c |author=Todd C. Miller |website=BSD Cross Reference}} and strlcpy{{cite web|url=http://bxr.su/OpenBSD/lib/libc/string/strlcat.c |title= strlcat.c |author=Todd C. Miller |website=BSD Cross Reference}} functions, which appeared in OpenBSD 2.4 in December, 1998. These functions always write one NUL to the destination buffer, truncating the result if necessary, and return the size of buffer that would be needed, which allows detection of the truncation and provides a size for creating a new buffer that will not truncate. For a long time they have not been included in the GNU C library (used by software on Linux), on the basis of allegedly being inefficient,{{cite web |url=https://www.openbsd.org/papers/portability.pdf |title=Secure Portability |first1=Damien |last1=Miller |date=October 2005|access-date=26 June 2016 |quote=This [strlcpy and strlcat] API has been adopted by most modern operating systems and many standalone software packages [...]. The notable exception is the GNU standard C library, glibc, whose maintainer steadfastly refuses to include these improved APIs, labelling them "horribly inefficient BSD crap", despite prior evidence that they are faster is most cases than the APIs they replace.}} encouraging the use of C strings (instead of some superior alternative form of string),[http://sources.redhat.com/ml/libc-alpha/ libc-alpha mailing list] {{Webarchive|url=https://web.archive.org/web/20070609043450/http://sources.redhat.com/ml/libc-alpha/ |date=9 June 2007 }}, selected messages from 8 August 2000 thread: [https://www.sourceware.org/ml/libc-alpha/2000-08/msg00053.html 53], [https://www.sourceware.org/ml/libc-alpha/2000-08/msg00060.html 60], [https://www.sourceware.org/ml/libc-alpha/2000-08/msg00061.html 61][https://lwn.net/Articles/507319/ The ups and downs of strlcpy(); LWN.net] and hiding other potential errors.{{cite web |title=Adding strlcpy() to glibc |url=https://lwn.net/Articles/612244/ |website=lwn.net |quote=Correct string handling means that you always know how long your strings are and therefore you can you memcpy (instead of strcpy).}}{{man|3|strlcpy|ManKier}} "However, one may question the validity of such optimizations, as they defeat the whole purpose of strlcpy() and strlcat(). As a matter of fact, the first version of this manual page got it wrong." Even while glibc hadn't added support, strlcat and strlcpy have been implemented in a number of other C libraries including ones for OpenBSD, FreeBSD, NetBSD, Solaris, OS X, and QNX, as well as in alternative C libraries for Linux, such as [https://en.wikibooks.org/wiki/C%20Programming/C%20Reference/nonstandard/strlcpy libbsd], introduced in 2008,{{Cite web |url=https://libbsd.freedesktop.org/ |title=libbsd |access-date=2022-11-21}} and musl, introduced in 2011,{{cite web |url=https://git.musl-libc.org/cgit/musl/tree/src/string/strlcpy.c |title=root/src/string/strlcpy.c |access-date=2017-01-28 }}{{cite web |url=https://git.musl-libc.org/cgit/musl/tree/src/string/strlcat.c |title=root/src/string/strlcat.c |access-date=2017-01-28 }} and the source code added directly to other projects such as SDL, GLib, ffmpeg, rsync, and even internally in the Linux kernel. This did change in 2024, the [https://sourceware.org/glibc/wiki/FAQ#Why_no_strlcpy_.2F_strlcat.3F glibc FAQ] notes that as of glibc 2.38, the code has been committed [https://sourceware.org/git/?p=glibc.git;a=commit;h=454a20c8756c9c1d55419153255fc7692b3d2199 strlc{py|at} commit] and thereby added.[https://news.ycombinator.com/item?id=36765747 Discussion of strlcpy and strlcat in glibc 2.38 on Hacker News] These functions were standardized as part of POSIX.1-2024,{{cite web|url=https://pubs.opengroup.org/onlinepubs/9799919799/functions/strlcat.html |title=strlcat |publisher=Pubs.opengroup.org |access-date=2024-09-05}} the Austin Group Defect Tracker [https://www.austingroupbugs.net/view.php?id=986 ID 986] tracked some discussion about such plans for POSIX.

Sometimes memcpy or memmove are used, as they may be more efficient than strcpy as they do not repeatedly check for NUL (this is less true on modern processors). Since they need a buffer length as a parameter, correct setting of this parameter can avoid buffer overflows.

As part of its 2004 Security Development Lifecycle, Microsoft introduced a family of "secure" functions including strcpy_s and strcat_s (along with many others).{{cite web|last1=Lovell|first1=Martyn|title=Repel Attacks on Your Code with the Visual Studio 2005 Safe C and C++ Libraries|url=https://msdn.microsoft.com/en-us/magazine/cc163794.aspx|access-date=13 February 2015}} These functions were standardized with some minor changes as part of the optional C11 (Annex K) proposed by ISO/IEC WDTR 24731.{{cite web |url=http://safeclib.sourceforge.net/ |title=The Safe C Library provides bound checking memory and string functions per ISO/IEC TR24731 |author=Safe C Library |publisher=Sourceforge | access-date=6 March 2013 }} These functions perform various checks including whether the string is too long to fit in the buffer. If the checks fail, a user-specified "runtime-constraint handler" function is called,{{cite web |title=The C11 standard draft |url=http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1548.pdf |access-date=13 February 2013 |location=§K.3.1.4p2}} which usually aborts the program.{{cite web |title=The C11 standard draft |url=http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1548.pdf |access-date=13 February 2013 |location=§K.3.6.1.1p4}}{{cite web|title=Parameter Validation|date=21 October 2022 |url=https://msdn.microsoft.com/en-us/library/ksazx244.aspx}} These functions attracted considerable criticism because initially they were implemented only on Windows and at the same time warning messages started to be produced by Microsoft Visual C++ suggesting use of these functions instead of standard ones. This has been speculated by some to be an attempt by Microsoft to lock developers into its platform.{{cite web |url=http://www.informit.com/blogs/blog.aspx?uk=Theyre-at-it-again |title=They're at it again |author=Danny Kalev |publisher=InformIT |access-date=10 November 2011 |archive-url=https://web.archive.org/web/20120115011928/http://www.informit.com/blogs/blog.aspx?uk=Theyre-at-it-again |archive-date=15 January 2012 |url-status=dead }} Experience with these functions has shown significant problems with their adoption and errors in usage, so the removal of Annex K was proposed for the next revision of the C standard.{{cite web |title=Field Experience With Annex K — Bounds Checking Interfaces |url=http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1967.htm |access-date=5 November 2015}} Usage of {{code|memset_s}} has been suggested as a way to avoid unwanted compiler optimizations.{{cite web |title=MSC06-C. Beware of compiler optimizations |url=https://wiki.sei.cmu.edu/confluence/display/c/MSC06-C.+Beware+of+compiler+optimizations |website=SEI CERT C Coding Standard}}{{man|3|memset_s|FreeBSD}}

See also

Notes

{{Notelist}}

References

{{Reflist|30em}}