C string handling

From Infogalactic: the planetary knowledge core
Jump to: navigation, search

<templatestyles src="Module:Hatnote/styles.css"></templatestyles>

Lua error in package.lua at line 80: module 'strict' not found.

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.

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 a contiguous sequence of code units terminated by the first zero code (\0, corresponding to the 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[1] 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.[2] 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.[3] 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.[1] 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.[citation needed]

The term length of a string is used in C to describe the number of bytes preceding the zero byte.[1] 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.

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.

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[512] = "φωωβαρ";(UTF-8) or wchar_t foo[512] = L"φωωβαρ"; (UTF-16 or UTF-32) is implementation defined,[4] 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.

Overview of functions

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.

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.

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, usually either 16 or 32 bits. No specific interpretation is specified for these code units; the C standard requires of a wchar_t only that it be at least as large as a char, not that it can actually store Unicode code points or UTF-16 code units.[5]
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.
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[6] wcscpy[7] Copies one string to another
strncpy[8] wcsncpy[9] Writes exactly n bytes/wchar_t, copying from source or adding nulls
strcat[10] wcscat[11] Appends one string to another
strncat[12] wcsncat[13] Appends no more than n bytes/wchar_t from one string to another
strxfrm[14] wcsxfrm[15] Transforms a string according to the current locale
String
examination
strlen[16] wcslen[17] Returns the length of the string
strcmp[18] wcscmp[19] Compares two strings (three-way comparison)
strncmp[20] wcsncmp[21] Compares a specific number of bytes/wchar_t in two strings
strcoll[22] wcscoll[23] Compares two strings according to the current locale
strchr[24] wcschr[25] Finds the first occurrence of a byte/wchar_t in a string
strrchr[26] wcsrchr[27] Finds the last occurrence of a byte/wchar_t in a string
strspn[28] wcsspn[29] Finds in a string the first occurrence of a byte/wchar_t not in a set
strcspn[30] wcscspn[31] Finds in a string the last occurrence of a byte/wchar_t not in a set
strpbrk[32] wcspbrk[33] Finds in a string the first occurrence of a byte/wchar_t in a set
strstr[34] wcsstr[35] Finds the first occurrence of a substring in a string
strtok[36] wcstok[37] Splits a string into tokens
Miscellaneous strerror[38] N/A Returns a string containing a message derived from an error code
Memory
manipulation
memset[39] wmemset[40] Fills a buffer with a repeated byte/wchar_t
memcpy[41] wmemcpy[42] Copies one buffer to another
memmove[43] wmemmove[44] Copies one buffer to another, possibly overlapping, buffer
memcmp[45] wmemcmp[46] Compares two buffers (three-way comparison)
memchr[47] wmemchr[48] Finds the first occurrence of a byte/wchar_t in a buffer
  1. Here string refers either to byte string or wide string

Multibyte functions

Name Description
mblen[49] Returns the number of bytes in the next multibyte character
mbtowc[50] Converts the next multibyte character to a wide character
wctomb[51] Converts a wide character to its multibyte representation
mbstowcs[52] Converts a multibyte string to a wide string
wcstombs[53] Converts a wide string to a multibyte string
btowc[54] Convert a single-byte character to wide character, if possible
wctob[55] Convert a wide character to a single-byte character, if possible
mbsinit[56] Checks if a state object represents initial state
mbrlen[57] Returns the number of bytes in the next multibyte character, given state
mbrtowc[58] Converts the next multibyte character to a wide character, given state
wcrtomb[59] Converts a wide character to its multibyte representation, given state
mbsrtowcs[60] Converts a multibyte string to a wide string, given state
wcsrtombs[61] 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.

Numeric conversions

Byte
string
Wide
string
Description[note 1]
atof[62] N/A converts a string to a floating-point value
atoi
atol
atoll[63]
N/A converts a string to an integer (C99)
strtof (C99)[64]
strtod[65]
strtold (C99)[66]
wcstof (C99)[67]
wcstod[68]
wcstold (C99)[69]
converts a string to a floating-point value
strtol
strtoll[70]
wcstol
wcstoll[71]
converts a string to a signed integer
strtoul
strtoull[72]
wcstoul
wcstoull[73]
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 strtoxxx functions are not const-correct, since they accept a const string pointer and return a non-const pointer within the string. 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.[74]

Popular extensions

Name Platform Description
memccpy[75] 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[76] GNU a variant of memcpy returning a pointer to the byte following the last written byte
strcasecmp[77] POSIX, BSD case-insensitive versions of strcmp
strcat_s[78] C (2011) and ISO/IEC WDTR 24731 a variant of strcat that checks the destination buffer size before copying
strcpy_s[79] C (2011) and ISO/IEC WDTR 24731 a variant of strcpy that checks the destination buffer size before copying
strdup[80] POSIX allocates and duplicates a string
strerror_r[81] POSIX 1, GNU a variant of strerror that is thread-safe. The GNU version is incompatible with the POSIX one.
stricmp[82] Various case-insensitive versions of strcmp
strlcpy[83] BSD, Solaris a variant of strcpy that truncates the result to fit in the destination buffer[84]
strlcat[83] BSD, Solaris a variant of strcat that truncates the result to fit in the destination buffer[84]
strsignal[85] POSIX:2008 returns string representation of a signal code. Not thread safe.
strtok_r[86] POSIX a variant of strtok that is thread-safe

Replacements

Despite the well-established need to replace strcat[10] and strcpy[6] 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.[84]

The most popular[lower-alpha 1] replacement are the strlcat and strlcpy functions, which appeared in OpenBSD 2.4 in December, 1998.[84] 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 encouraging the use of C strings and creating more problems than initially trying to solve.[87][88] 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, OS X, and QNX. The lack of GNU C library support has not stopped various library 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.[89][90]

As part of its 2004 Security Development Lifecycle, Microsoft introduced a family of "secure" functions, such as strcpy_s and strcat_s (along with many others);[91] these functions were later standardized with some minor changes, and are now part of the optional C11 (Annex K) as proposed by ISO/IEC WDTR 24731. These functions perform runtime integrity checks of their arguments; if the checks fail, a user-specified "runtime-constraint handler" function is called.[92] If the user has not specified such a function, the default behavior is implementation-defined.[93] Microsoft's C runtime will abort the program when the constraints are violated.[94] Some functions perform destructive operations before calling the runtime-constraint handler; for example, strcat_s sets the destination to the empty string,[95] 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 the programmers to use these functions instead of standard ones. This has been speculated by some to be an attempt by Microsoft to lock developers into its platform.[96] Although open-source implementations of these functions are available,[97] these functions are not present in common Unix C libraries. Experience with the safe functions has shown significant problems with their adoption and the removal of the optional C11 (Annex K) is proposed for the next revision of the standard[98]

If the string length is known, then memcpy[41] or memmove[43] can be more efficient than strcpy[citation needed], so some programs[who?] use them to optimize C string manipulation. They accept a buffer length as a parameter, so they can be employed to prevent buffer overflows in a manner similar to the aforementioned functions.

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 Lua error in package.lua at line 80: module 'strict' not found.
  2. Lua error in package.lua at line 80: module 'strict' not found.
  3. Lua error in package.lua at line 80: module 'strict' not found.
  4. Lua error in package.lua at line 80: module 'strict' not found.
  5. Lua error in package.lua at line 80: module 'strict' not found.
  6. 6.0 6.1 Lua error in package.lua at line 80: module 'strict' not found.
  7. Lua error in package.lua at line 80: module 'strict' not found.
  8. Lua error in package.lua at line 80: module 'strict' not found.
  9. Lua error in package.lua at line 80: module 'strict' not found.
  10. 10.0 10.1 Lua error in package.lua at line 80: module 'strict' not found.
  11. Lua error in package.lua at line 80: module 'strict' not found.
  12. Lua error in package.lua at line 80: module 'strict' not found.
  13. Lua error in package.lua at line 80: module 'strict' not found.
  14. Lua error in package.lua at line 80: module 'strict' not found.
  15. Lua error in package.lua at line 80: module 'strict' not found.
  16. Lua error in package.lua at line 80: module 'strict' not found.
  17. Lua error in package.lua at line 80: module 'strict' not found.
  18. Lua error in package.lua at line 80: module 'strict' not found.
  19. Lua error in package.lua at line 80: module 'strict' not found.
  20. Lua error in package.lua at line 80: module 'strict' not found.
  21. Lua error in package.lua at line 80: module 'strict' not found.
  22. Lua error in package.lua at line 80: module 'strict' not found.
  23. Lua error in package.lua at line 80: module 'strict' not found.
  24. Lua error in package.lua at line 80: module 'strict' not found.
  25. Lua error in package.lua at line 80: module 'strict' not found.
  26. Lua error in package.lua at line 80: module 'strict' not found.
  27. Lua error in package.lua at line 80: module 'strict' not found.
  28. Lua error in package.lua at line 80: module 'strict' not found.
  29. Lua error in package.lua at line 80: module 'strict' not found.
  30. Lua error in package.lua at line 80: module 'strict' not found.
  31. Lua error in package.lua at line 80: module 'strict' not found.
  32. Lua error in package.lua at line 80: module 'strict' not found.
  33. Lua error in package.lua at line 80: module 'strict' not found.
  34. Lua error in package.lua at line 80: module 'strict' not found.
  35. Lua error in package.lua at line 80: module 'strict' not found.
  36. Lua error in package.lua at line 80: module 'strict' not found.
  37. Lua error in package.lua at line 80: module 'strict' not found.
  38. Lua error in package.lua at line 80: module 'strict' not found.
  39. Lua error in package.lua at line 80: module 'strict' not found.
  40. Lua error in package.lua at line 80: module 'strict' not found.
  41. 41.0 41.1 Lua error in package.lua at line 80: module 'strict' not found.
  42. Lua error in package.lua at line 80: module 'strict' not found.
  43. 43.0 43.1 Lua error in package.lua at line 80: module 'strict' not found.
  44. Lua error in package.lua at line 80: module 'strict' not found.
  45. Lua error in package.lua at line 80: module 'strict' not found.
  46. Lua error in package.lua at line 80: module 'strict' not found.
  47. Lua error in package.lua at line 80: module 'strict' not found.
  48. Lua error in package.lua at line 80: module 'strict' not found.
  49. Lua error in package.lua at line 80: module 'strict' not found.
  50. Lua error in package.lua at line 80: module 'strict' not found.
  51. Lua error in package.lua at line 80: module 'strict' not found.
  52. Lua error in package.lua at line 80: module 'strict' not found.
  53. Lua error in package.lua at line 80: module 'strict' not found.
  54. Lua error in package.lua at line 80: module 'strict' not found.
  55. Lua error in package.lua at line 80: module 'strict' not found.
  56. Lua error in package.lua at line 80: module 'strict' not found.
  57. Lua error in package.lua at line 80: module 'strict' not found.
  58. Lua error in package.lua at line 80: module 'strict' not found.
  59. Lua error in package.lua at line 80: module 'strict' not found.
  60. Lua error in package.lua at line 80: module 'strict' not found.
  61. Lua error in package.lua at line 80: module 'strict' not found.
  62. Lua error in package.lua at line 80: module 'strict' not found.
  63. Lua error in package.lua at line 80: module 'strict' not found.
  64. Lua error in package.lua at line 80: module 'strict' not found.
  65. Lua error in package.lua at line 80: module 'strict' not found.
  66. Lua error in package.lua at line 80: module 'strict' not found.
  67. Lua error in package.lua at line 80: module 'strict' not found.
  68. Lua error in package.lua at line 80: module 'strict' not found.
  69. Lua error in package.lua at line 80: module 'strict' not found.
  70. Lua error in package.lua at line 80: module 'strict' not found.
  71. Lua error in package.lua at line 80: module 'strict' not found.
  72. Lua error in package.lua at line 80: module 'strict' not found.
  73. Lua error in package.lua at line 80: module 'strict' not found.
  74. C99 Rationale, 7.20.1.1
  75. Lua error in package.lua at line 80: module 'strict' not found.
  76. Lua error in package.lua at line 80: module 'strict' not found.
  77. Lua error in package.lua at line 80: module 'strict' not found.
  78. Lua error in package.lua at line 80: module 'strict' not found.
  79. Lua error in package.lua at line 80: module 'strict' not found.
  80. Lua error in package.lua at line 80: module 'strict' not found.
  81. Lua error in package.lua at line 80: module 'strict' not found.
  82. Lua error in package.lua at line 80: module 'strict' not found.
  83. 83.0 83.1 Lua error in package.lua at line 80: module 'strict' not found.
  84. 84.0 84.1 84.2 84.3 Lua error in package.lua at line 80: module 'strict' not found.
  85. Lua error in package.lua at line 80: module 'strict' not found.
  86. Lua error in package.lua at line 80: module 'strict' not found.
  87. libc-alpha mailing list, selected messages from 8 August 2000 thread: 53, 60, 61
  88. The ups and downs of strlcpy(); LWN.net
  89. Lua error in package.lua at line 80: module 'strict' not found.
  90. Lua error in package.lua at line 80: module 'strict' not found.
  91. Lua error in package.lua at line 80: module 'strict' not found.
  92. Lua error in package.lua at line 80: module 'strict' not found.
  93. Lua error in package.lua at line 80: module 'strict' not found.
  94. Lua error in package.lua at line 80: module 'strict' not found.
  95. Lua error in package.lua at line 80: module 'strict' not found.
  96. Lua error in package.lua at line 80: module 'strict' not found.
  97. Lua error in package.lua at line 80: module 'strict' not found.
  98. Lua error in package.lua at line 80: module 'strict' not found.

External links

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

es:String.h

fr:String.h ko:String.h it:String.h pt:String.h ru:String.h uk:String.h zh:String.h