C string handling

From HandWiki
Short description: Handling of strings in the C programming language

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 n characters is represented as an array of n + 1 elements, the last of which is a "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).[1] 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.[1] 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[1] 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.[2] 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.[3]

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 (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++.)[4] C++11 and C11 add two types with explicit widths char16_t and char32_t.[5]

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 char foo[512] = "φωωβαρ"; (UTF-8) or wchar_t foo[512] = L"φωωβαρ"; (UTF-16 or UTF-32, depends on wchar_t) is implementation defined,[6] and may require that the source code be in the same encoding, especially for 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 u8 is available that guarantees UTF-8 for a bytestring literal, as in char foo[512] = u8"φωωβαρ";.[7] 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?] 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
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.
wchar_t Type used for a code unit in "wide" strings. On Windows, the only platform to use wchar_t extensively, it's defined as 16-bit[8] 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 wchar_t be wide enough to hold the widest character set among the supported system locales[9] and be greater or equal in size to char,[10]
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.
char8_t[11] Part of the C standard since C23, in <uchar.h>, a type that is suitable for storing UTF-8 characters.[12]
char16_t[13] Part of the C standard since C11,[14] in <uchar.h>, a type capable of holding 16 bits even if 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.[15] C++ does not define such a macro, but the type is always used for UTF-16 in that language.[16]
char32_t[13] Part of the C standard since C11,[17] in <uchar.h>, a type capable of holding 32 bits even if 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. [15] C++ does not define such a macro, but the type is always used for UTF-32 in that language.[16]
mbstate_t Contains all the information about the conversion state required from one call to a function to the other.

Functions

Byte
string
Wide
string
Description[note 1]
String
manipulation
strcpy[18] wcscpy[19] Copies one string to another
strncpy[20] wcsncpy[21] Writes exactly n bytes, copying from source or adding nulls
strcat[22] wcscat[23] Appends one string to another
strncat[24] wcsncat[25] Appends no more than n bytes from one string to another
strxfrm[26] wcsxfrm[27] Transforms a string according to the current locale
String
examination
strlen[28] wcslen[29] Returns the length of the string
strcmp[30] wcscmp[31] Compares two strings (three-way comparison)
strncmp[32] wcsncmp[33] Compares a specific number of bytes in two strings
strcoll[34] wcscoll[35] Compares two strings according to the current locale
strchr[36] wcschr[37] Finds the first occurrence of a byte in a string
strrchr[38] wcsrchr[39] Finds the last occurrence of a byte in a string
strspn[40] wcsspn[41] Returns the number of initial bytes in a string that are in a second string
strcspn[42] wcscspn[43] Returns the number of initial bytes in a string that are not in a second string
strpbrk[44] wcspbrk[45] Finds in a string the first occurrence of a byte in a set
strstr[46] wcsstr[47] Finds the first occurrence of a substring in a string
strtok[48] wcstok[49] Splits a string into tokens
Miscellaneous strerror[50] N/A Returns a string containing a message derived from an error code
Memory
manipulation
memset[51] wmemset[52] Fills a buffer with a repeated byte. Since C23, memset_explicit() was added to erase sensitive data.
memcpy[53] wmemcpy[54] Copies one buffer to another. Since C23, memccpy() was added to efficiently concatenate strings.
memmove[55] wmemmove[56] Copies one buffer to another, possibly overlapping, buffer
memcmp[57] wmemcmp[58] Compares two buffers (three-way comparison)
memchr[59] wmemchr[60] Finds the first occurrence of a byte in a buffer
  1. For wide string functions substitute wchar_t for "byte" in the description

Multibyte functions

Name Description
mblen[61] Returns the number of bytes in the next multibyte character
mbtowc[62] Converts the next multibyte character to a wide character
wctomb[63] Converts a wide character to its multibyte representation
mbstowcs[64] Converts a multibyte string to a wide string
wcstombs[65] Converts a wide string to a multibyte string
btowc[66] Converts a single-byte character to wide character, if possible
wctob[67] Converts a wide character to a single-byte character, if possible
mbsinit[68] Checks if a state object represents initial state
mbrlen[69] Returns the number of bytes in the next multibyte character, given state
mbrtowc[70] Converts the next multibyte character to a wide character, given state
wcrtomb[71] Converts a wide character to its multibyte representation, given state
mbsrtowcs[72] Converts a multibyte string to a wide string, given state
wcsrtombs[73] Converts a wide string to a multibyte string, given state
mbrtoc8[74] Converts the next multibyte character to a UTF-8 character, given state
c8rtomb[75] Converts a single code point from UTF-8 to a narrow multibyte character representation, given state
mbrtoc16[76] Converts the next multibyte character to a UTF-16 character, given state
c16rtomb[77] Converts a single code point from UTF-16 to a narrow multibyte character representation, given state
mbrtoc32[78] Converts the next multibyte character to a UTF-32 character, given state
c32rtomb[79] Converts a single code point from UTF-32 to a narrow multibyte character representation, given state

These functions all need a 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 mb encodings, but modern ones such as UTF-8 do not need this. However these functions were designed on the assumption that the wc encoding is not a variable-width encoding and thus are designed to deal with exactly one wchar_t at a time, passing it by value rather than using a string pointer. As UTF-16 is a variable-width encoding, the mbstate_t has been reused to keep track of surrogate pairs in the wide encoding, though the caller must still detect and call mbtowc twice for a single character.[80][81][82] 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

Byte
string
Wide
string
Description[note 1]
atof[83] N/A converts a string to a floating-point value ('atof' means 'ASCII to float')
atoi
atol
atoll[84]
N/A converts a string to an integer (C99) ('atoi' means 'ASCII to integer')
strtof (C99)[85]
strtod[86]
strtold (C99)[87]
wcstof (C99)[88]
wcstod[89]
wcstold (C99)[90]
converts a string to a floating-point value
strtol
strtoll[91]
wcstol
wcstoll[92]
converts a string to a signed integer
strtoul
strtoull[93]
wcstoul
wcstoull[94]
converts a string to an unsigned integer
  1. Here string refers either to byte string or wide string

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++).

The functions strchr, bsearch, strpbrk, strrchr, strstr, memchr and their wide counterparts are not const-correct, since they accept a const string pointer and return a non-const pointer within the string. This has been fixed in C23.[95]

Also, since the Normative Amendment 1 (C95), atoxx functions are considered subsumed by strtoxxx functions, for which reason neither C95 nor any later standard provides wide-character versions of these functions. The argument against atoxx is that they do not differentiate between an error and a 0.[96]

Popular extensions

Name Platform Description
bzero[97][98] POSIX, BSD Fills a buffer with zero bytes, deprecated by memset
memccpy[99] SVID, POSIX Part of the C standard since C23, copies between two non-overlapping memory areas, stopping when a given byte is found.
mempcpy[100] GNU a variant of memcpy returning a pointer to the byte following the last written byte
strcasecmp[101] POSIX, BSD case-insensitive versions of strcmp
strcat_s[102] Windows a variant of strcat that checks the destination buffer size before copying
strcpy_s[103] Windows a variant of strcpy that checks the destination buffer size before copying
strdup & strndup[104] POSIX Part of the C standard since C23, allocates and duplicates a string
strerror_r[105] POSIX 1, GNU a variant of strerror that is thread-safe. The GNU version is incompatible with the POSIX one.
stricmp[106] Windows case-insensitive versions of strcmp
strlcpy[107] BSD, Solaris a variant of strcpy that truncates the result to fit in the destination buffer[108]
strlcat[107] BSD, Solaris a variant of strcat that truncates the result to fit in the destination buffer[108]
strsignal[109] POSIX:2008 returns string representation of a signal code. Not thread safe.
strtok_r[110] POSIX a variant of strtok that is thread-safe

Replacements

Despite the well-established need to replace strcat[22] and strcpy[18] 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.[108]

The most popular[lower-alpha 1] replacement are the strlcat and strlcpy functions, which appeared in OpenBSD 2.4 in December, 1998.[108] 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. They have been criticized on the basis of allegedly being inefficient,[111] encouraging the use of C strings (instead of some superior alternative form of string),[112][113] and hiding other potential errors.[114][115] Consequently, they have not been included in the GNU C library (used by software on Linux), although they are implemented in the C libraries for OpenBSD, FreeBSD, NetBSD, Solaris, OS X, and QNX, as well as in alternative C libraries for Linux, such as libbsd, introduced in 2008,[116] and musl, introduced in 2011.[117][118] The lack of GNU C library support has not stopped various software authors from using it and bundling a replacement, among other SDL, GLib, ffmpeg, rsync, and even internally in the Linux kernel. Open source implementations for these functions are available.[119][120]

Sometimes memcpy[53] or memmove[55] 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).[121] These functions were standardized with some minor changes as part of the optional C11 (Annex K) proposed by ISO/IEC WDTR 24731. 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,[122] which usually aborts the program.[123][124] Some functions perform destructive operations before calling the runtime-constraint handler; for example, strcat_s sets the destination to the empty string,[125] which can make it difficult to recover from error conditions or debug them. 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.[126] Although open-source implementations of these functions are available, these functions are not present in common Unix C libraries.[127] Experience with these functions has shown significant problems with their adoption and errors in usage, so the removal of Annex K is proposed for the next revision of the C standard.[128] Usage of memset_s has also been suggested as a way to avoid unwanted compiler optimizations.[129][130]

See also

Notes

  1. 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]

References

  1. 1.0 1.1 1.2 "The C99 standard draft + TC3". §7.1.1p1. http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf. 
  2. "The C99 standard draft + TC3". §6.4.5p7. http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf. 
  3. "The C99 standard draft + TC3". Section 6.4.5 footnote 66. http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf. 
  4. "Relax requirements on wchar_t to match existing practices". https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2022/p2460r2.pdf. 
  5. "Fundamental types". https://en.cppreference.com/w/cpp/language/types. 
  6. "The C99 standard draft + TC3". §5.1.1.2 Translation phases, p1. http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf. 
  7. "string literals". https://en.cppreference.com/w/c/language/string_literal. 
  8. "c++ - What is the use of wchar_t in general programming?" (in en). https://stackoverflow.com/questions/13509733/what-is-the-use-of-wchar-t-in-general-programming. 
  9. "stddef.h - standard type definitions". The Open Group. https://pubs.opengroup.org/onlinepubs/007908775/xsh/stddef.h.html. 
  10. Gillam, Richard (2003). Unicode Demystified: A Practical Programmer's Guide to the Encoding Standard. Addison-Wesley Professional. p. 714. ISBN 9780201700527. https://books.google.com/books?id=wn5sXG8bEAcC&pg=PA714. 
  11. "char, wchar_t, char8_t, char16_t, char32_t" (in en-us). https://docs.microsoft.com/en-us/cpp/cpp/char-wchar-t-char16-t-char32-t. 
  12. "char8_t". https://en.cppreference.com/w/c/string/multibyte/char8_t. 
  13. 13.0 13.1 "<cuchar> (uchar.h)" (in en-us). https://cplusplus.com/reference/cuchar/. 
  14. "char16_t". https://en.cppreference.com/w/c/string/multibyte/char16_t. 
  15. 15.0 15.1 "Replacing text macros". https://en.cppreference.com/w/c/preprocessor/replace#Predefined_macros. 
  16. 16.0 16.1 "Fundamental types". https://en.cppreference.com/w/cpp/language/types#Character_types. 
  17. "char32_t". https://en.cppreference.com/w/c/string/multibyte/char32_t. 
  18. 18.0 18.1 "strcpy - cppreference.com". En.cppreference.com. 2014-01-02. https://en.cppreference.com/w/c/string/byte/strcpy. 
  19. "wcscpy - cppreference.com". En.cppreference.com. https://en.cppreference.com/w/c/string/wide/wcscpy. 
  20. "strncpy - cppreference.com". En.cppreference.com. 2013-10-04. https://en.cppreference.com/w/c/string/byte/strncpy. 
  21. "wcsncpy - cppreference.com". En.cppreference.com. https://en.cppreference.com/w/c/string/wide/wcsncpy. 
  22. 22.0 22.1 "strcat - cppreference.com". En.cppreference.com. 2013-10-08. https://en.cppreference.com/w/c/string/byte/strcat. 
  23. "wcscat - cppreference.com". En.cppreference.com. https://en.cppreference.com/w/c/string/wide/wcscat. 
  24. "strncat - cppreference.com". En.cppreference.com. 2013-07-01. https://en.cppreference.com/w/c/string/byte/strncat. 
  25. "wcsncat - cppreference.com". En.cppreference.com. https://en.cppreference.com/w/c/string/wide/wcsncat. 
  26. "strxfrm - cppreference.com". En.cppreference.com. https://en.cppreference.com/w/c/string/byte/strxfrm. 
  27. "wcsxfrm - cppreference.com". En.cppreference.com. https://en.cppreference.com/w/c/string/wide/wcsxfrm. 
  28. "strlen - cppreference.com". En.cppreference.com. 2013-12-27. https://en.cppreference.com/w/c/string/byte/strlen. 
  29. "wcslen - cppreference.com". En.cppreference.com. https://en.cppreference.com/w/c/string/wide/wcslen. 
  30. "strcmp - cppreference.com". En.cppreference.com. https://en.cppreference.com/w/c/string/byte/strcmp. 
  31. "wcscmp - cppreference.com". En.cppreference.com. https://en.cppreference.com/w/c/string/wide/wcscmp. 
  32. "strncmp - cppreference.com". En.cppreference.com. https://en.cppreference.com/w/c/string/byte/strncmp. 
  33. "wcsncmp - cppreference.com". En.cppreference.com. https://en.cppreference.com/w/c/string/wide/wcsncmp. 
  34. "strcoll - cppreference.com". En.cppreference.com. https://en.cppreference.com/w/c/string/byte/strcoll. 
  35. "wcscoll - cppreference.com". En.cppreference.com. https://en.cppreference.com/w/c/string/wide/wcscoll. 
  36. "strchr - cppreference.com". En.cppreference.com. 2014-02-23. https://en.cppreference.com/w/c/string/byte/strchr. 
  37. "wcschr - cppreference.com". En.cppreference.com. https://en.cppreference.com/w/c/string/wide/wcschr. 
  38. "strrchr - cppreference.com". En.cppreference.com. https://en.cppreference.com/w/c/string/byte/strrchr. 
  39. "wcsrchr - cppreference.com". En.cppreference.com. https://en.cppreference.com/w/c/string/wide/wcsrchr. 
  40. "strspn - cppreference.com". En.cppreference.com. https://en.cppreference.com/w/c/string/byte/strspn. 
  41. "wcsspn - cppreference.com". En.cppreference.com. https://en.cppreference.com/w/c/string/wide/wcsspn. 
  42. "strcspn - cppreference.com". En.cppreference.com. 2013-05-31. https://en.cppreference.com/w/c/string/byte/strcspn. 
  43. "wcscspn - cppreference.com". En.cppreference.com. https://en.cppreference.com/w/c/string/wide/wcscspn. 
  44. "strpbrk - cppreference.com". En.cppreference.com. 2013-05-31. https://en.cppreference.com/w/c/string/byte/strpbrk. 
  45. "wcspbrk - cppreference.com". En.cppreference.com. https://en.cppreference.com/w/c/string/wide/wcspbrk. 
  46. "strstr - cppreference.com". En.cppreference.com. 2013-10-16. https://en.cppreference.com/w/c/string/byte/strstr. 
  47. "wcsstr - cppreference.com". En.cppreference.com. https://en.cppreference.com/w/c/string/wide/wcsstr. 
  48. "strtok - cppreference.com". En.cppreference.com. 2013-09-03. https://en.cppreference.com/w/c/string/byte/strtok. 
  49. "wcstok - cppreference.com". En.cppreference.com. https://en.cppreference.com/w/c/string/wide/wcstok. 
  50. "strerror - cppreference.com". En.cppreference.com. 2013-05-31. https://en.cppreference.com/w/c/string/byte/strerror. 
  51. "memset - cppreference.com". En.cppreference.com. https://en.cppreference.com/w/c/string/byte/memset. 
  52. "wmemset - cppreference.com". En.cppreference.com. https://en.cppreference.com/w/c/string/wide/wmemset. 
  53. 53.0 53.1 "memcpy - cppreference.com". En.cppreference.com. https://en.cppreference.com/w/c/string/byte/memcpy. 
  54. "wmemcpy - cppreference.com". En.cppreference.com. https://en.cppreference.com/w/c/string/wide/wmemcpy. 
  55. 55.0 55.1 "memmove - cppreference.com". En.cppreference.com. 2014-01-25. https://en.cppreference.com/w/c/string/byte/memmove. 
  56. "wmemmove - cppreference.com". En.cppreference.com. https://en.cppreference.com/w/c/string/wide/wmemmove. 
  57. "memcmp - cppreference.com". En.cppreference.com. https://en.cppreference.com/w/c/string/byte/memcmp. 
  58. "wmemcmp - cppreference.com". En.cppreference.com. https://en.cppreference.com/w/c/string/wide/wmemcmp. 
  59. "memchr - cppreference.com". En.cppreference.com. https://en.cppreference.com/w/c/string/byte/memchr. 
  60. "wmemchr - cppreference.com". En.cppreference.com. https://en.cppreference.com/w/c/string/wide/wmemchr. 
  61. "mblen - cppreference.com". En.cppreference.com. https://en.cppreference.com/w/c/string/multibyte/mblen. 
  62. "mbtowc - cppreference.com". En.cppreference.com. https://en.cppreference.com/w/c/string/multibyte/mbtowc. 
  63. "wctomb - cppreference.com". En.cppreference.com. 2014-02-04. https://en.cppreference.com/w/c/string/multibyte/wctomb. 
  64. "mbstowcs - cppreference.com". En.cppreference.com. https://en.cppreference.com/w/c/string/multibyte/mbstowcs. 
  65. "wcstombs - cppreference.com". En.cppreference.com. https://en.cppreference.com/w/c/string/multibyte/wcstombs. 
  66. "btowc - cppreference.com". En.cppreference.com. https://en.cppreference.com/w/c/string/multibyte/btowc. 
  67. "wctob - cppreference.com". En.cppreference.com. https://en.cppreference.com/w/c/string/multibyte/wctob. 
  68. "mbsinit - cppreference.com". En.cppreference.com. https://en.cppreference.com/w/c/string/multibyte/mbsinit. 
  69. "mbrlen - cppreference.com". En.cppreference.com. https://en.cppreference.com/w/c/string/multibyte/mbrlen. 
  70. "mbrtowc - cppreference.com". En.cppreference.com. https://en.cppreference.com/w/c/string/multibyte/mbrtowc. 
  71. "wcrtomb - cppreference.com". En.cppreference.com. https://en.cppreference.com/w/c/string/multibyte/wcrtomb. 
  72. "mbsrtowcs - cppreference.com". En.cppreference.com. https://en.cppreference.com/w/c/string/multibyte/mbsrtowcs. 
  73. "wcsrtombs - cppreference.com". En.cppreference.com. https://en.cppreference.com/w/c/string/multibyte/wcsrtombs. 
  74. "mbrtoc8 - cppreference.com". En.cppreference.com. https://en.cppreference.com/w/c/string/multibyte/mbrtoc8. 
  75. "c8rtomb - cppreference.com". En.cppreference.com. https://en.cppreference.com/w/c/string/multibyte/c8rtomb. 
  76. "mbrtoc16 - cppreference.com". En.cppreference.com. https://en.cppreference.com/w/c/string/multibyte/mbrtoc16. 
  77. "c16rtomb - cppreference.com". En.cppreference.com. https://en.cppreference.com/w/c/string/multibyte/c16rtomb. 
  78. "mbrtoc32 - cppreference.com". En.cppreference.com. https://en.cppreference.com/w/c/string/multibyte/mbrtoc32. 
  79. "c23rtomb - cppreference.com". En.cppreference.com. https://en.cppreference.com/w/c/string/multibyte/c32rtomb. 
  80. "6.3.2 Representing the state of the conversion". https://www.gnu.org/software/libc/manual/html_node/Keeping-the-state.html. 
  81. "root/src/multibyte/c16rtomb.c". https://git.musl-libc.org/cgit/musl/tree/src/multibyte/c16rtomb.c. 
  82. "Contents of /stable/11/lib/libc/locale/c16rtomb.c". https://svnweb.freebsd.org/base/stable/11/lib/libc/locale/c16rtomb.c?view=markup. 
  83. "atof - cppreference.com". En.cppreference.com. 2013-05-31. https://en.cppreference.com/w/c/string/byte/atof. 
  84. "atoi, atol, atoll - cppreference.com". En.cppreference.com. 2014-01-18. https://en.cppreference.com/w/c/string/byte/atoi. 
  85. "strtof, strtod, strtold - cppreference.com". En.cppreference.com. 2014-02-04. https://en.cppreference.com/w/c/string/byte/strtof. 
  86. "strtof, strtod, strtold - cppreference.com". En.cppreference.com. 2014-02-04. https://en.cppreference.com/w/c/string/byte/strtod. 
  87. "strtof, strtod, strtold - cppreference.com". En.cppreference.com. 2014-02-04. https://en.cppreference.com/w/c/string/byte/strtold. 
  88. "wcstof, wcstod, wcstold - cppreference.com". En.cppreference.com. https://en.cppreference.com/w/c/string/wide/wcstof. 
  89. "wcstof, wcstod, wcstold - cppreference.com". En.cppreference.com. https://en.cppreference.com/w/c/string/wide/wcstod. 
  90. "wcstof, wcstod, wcstold - cppreference.com". En.cppreference.com. http://en.cppreference.com/w/c/string/wide/wcstold. 
  91. "strtol, strtoll - cppreference.com". En.cppreference.com. 2014-02-04. https://en.cppreference.com/w/c/string/byte/strtol. 
  92. "wcstol, wcstoll - cppreference.com". En.cppreference.com. https://en.cppreference.com/w/c/string/wide/wcstol. 
  93. "strtoul, strtoull - cppreference.com". En.cppreference.com. 2014-02-04. https://en.cppreference.com/w/c/string/byte/strtoul. 
  94. "wcstoul, wcstoull - cppreference.com". En.cppreference.com. https://en.cppreference.com/w/c/string/wide/wcstoul. 
  95. "WG14-N3020 : Qualifier-preserving standard library functions, v4". 2022-06-13. https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3020.pdf. 
  96. C99 Rationale, 7.20.1.1
  97. "bzero". The Open Group. http://pubs.opengroup.org/onlinepubs/009695399/functions/bzero.html. 
  98. "bzero(3)". OpenBSD. https://man.openbsd.org/explicit_bzero.3. 
  99. "memccpy". Pubs.opengroup.org. http://pubs.opengroup.org/onlinepubs/009695399/functions/memccpy.html. 
  100. "mempcpy(3) - Linux manual page". Kernel.org. https://www.kernel.org/doc/man-pages/online/pages/man3/mempcpy.3.html. 
  101. "strcasecmp(3) - Linux manual page". Kernel.org. https://www.kernel.org/doc/man-pages/online/pages/man3/strcasecmp.3.html. 
  102. "strcat_s, wcscat_s, _mbscat_s". docs.microsoft.com. https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/strcat-s-wcscat-s-mbscat-s?view=msvc-170. 
  103. "strcpy_s, wcscpy_s, _mbscpy_s, _mbscpy_s_l". docs.microsoft.com. https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/strcpy-s-wcscpy-s-mbscpy-s?view=msvc-170. 
  104. "strdup". Pubs.opengroup.org. http://pubs.opengroup.org/onlinepubs/009695399/functions/strdup.html. 
  105. "strerror(3) - Linux manual page". man7.org. http://man7.org/linux/man-pages/man3/strerror.3.html. 
  106. "String | stricmp()". C Programming Expert.com. http://www.cprogrammingexpert.com/C/Tutorial/strings/stricmp.aspx. 
  107. 107.0 107.1 "strlcpy, strlcat — size-bounded string copying and concatenation". OpenBSD. https://man.openbsd.org/OpenBSD-5.9/man3/strlcat.3. 
  108. 108.0 108.1 108.2 108.3 Todd C. Miller; Theo de Raadt (1999). "strlcpy and strlcat – consistent, safe, string copy and concatenation.". USENIX '99. https://www.sudo.ws/todd/papers/strlcpy.html. 
  109. "strsignal". Pubs.opengroup.org. https://pubs.opengroup.org/onlinepubs/9699919799/functions/strsignal.html. 
  110. "strtok". Pubs.opengroup.org. https://pubs.opengroup.org/onlinepubs/009695399/functions/strtok.html. 
  111. Miller, Damien (October 2005). "Secure Portability". https://www.openbsd.org/papers/portability.pdf. "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." 
  112. libc-alpha mailing list , selected messages from 8 August 2000 thread: 53, 60, 61
  113. The ups and downs of strlcpy(); LWN.net
  114. "Adding strlcpy() to glibc". https://lwn.net/Articles/612244/. "Correct string handling means that you always know how long your strings are and therefore you can you memcpy (instead of strcpy)." 
  115. strlcpy(3) – Linux Library Functions Manual "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."
  116. "libbsd". https://libbsd.freedesktop.org/. 
  117. "root/src/string/strlcpy.c". https://git.musl-libc.org/cgit/musl/tree/src/string/strlcpy.c. 
  118. "root/src/string/strlcat.c". https://git.musl-libc.org/cgit/musl/tree/src/string/strlcat.c. 
  119. Todd C. Miller. "strlcpy.c". http://bxr.su/OpenBSD/lib/libc/string/strlcpy.c. 
  120. Todd C. Miller. "strlcat.c". http://bxr.su/OpenBSD/lib/libc/string/strlcat.c. 
  121. Lovell, Martyn. "Repel Attacks on Your Code with the Visual Studio 2005 Safe C and C++ Libraries". https://msdn.microsoft.com/en-us/magazine/cc163794.aspx. 
  122. "The C11 standard draft". §K.3.1.4p2. http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1548.pdf. 
  123. "The C11 standard draft". §K.3.6.1.1p4. http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1548.pdf. 
  124. "Parameter Validation". https://msdn.microsoft.com/en-us/library/ksazx244.aspx. 
  125. "The C11 standard draft". §K.3.7.2.1p4. http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1548.pdf. 
  126. Danny Kalev. "They're at it again". InformIT. http://www.informit.com/blogs/blog.aspx?uk=Theyre-at-it-again. 
  127. Safe C Library. "The Safe C Library provides bound checking memory and string functions per ISO/IEC TR24731". Sourceforge. http://safeclib.sourceforge.net/. 
  128. "Field Experience With Annex K — Bounds Checking Interfaces". http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1967.htm. 
  129. "MSC06-C. Beware of compiler optimizations". https://wiki.sei.cmu.edu/confluence/display/c/MSC06-C.+Beware+of+compiler+optimizations. 
  130. memset_s(3) – FreeBSD Library Functions Manual

External links

  • Fast memcpy in C, multiple C coding examples to target different types of CPU instruction architectures