请输入您要查询的百科知识:

 

词条 C string handling
释义

  1. Definitions

  2. Character encodings

  3. Overview of functions

     Constants and types  Functions  Multibyte functions  {{anchor|stdlib.h}}Numeric conversions 

  4. Popular extensions

  5. Replacements

  6. See also

  7. Notes

  8. References

  9. External links

{{redirect|C string}}{{use dmy dates|date=January 2012}}{{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 "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 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 if you see the term string it 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 w_char* 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

{{refimprove section|date=January 2017}}

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{{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.

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}}wchar_twchar_t}} be wide enough to hold the widest character set among the supported system locales.[5] Theoretically, {{mono|wchar_t}} can be the same size as {{mono|char}}, and thus not capable of holding UTF-32 or UTF-16 code units.[6]
wint_t}}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}}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[7]
String
manipulation
strcpy}}strcpy[8]wcscpy}}wcscpy[9] Copies one string to another
strncpy}}strncpy[10]wcsncpy}}wcsncpy[11]n}} bytes, copying from source or adding nulls
strcat}}strcat[12]wcscat}}wcscat[13] Appends one string to another
strncat}}strncat[14]wcsncat}}wcsncat[15]n}} bytes from one string to another
strxfrm}}strxfrm[16]wcsxfrm}}wcsxfrm[17] Transforms a string according to the current locale
String
examination
strlen}}strlen[18]wcslen}}wcslen[19] Returns the length of the string
strcmp}}strcmp[20]wcscmp}}wcscmp[21] Compares two strings (three-way comparison)
strncmp}}strncmp[22]wcsncmp}}wcsncmp[23] Compares a specific number of bytes in two strings
strcoll}}strcoll[24]wcscoll}}wcscoll[25] Compares two strings according to the current locale
strchr}}strchr[26]wcschr}}wcschr[27] Finds the first occurrence of a byte in a string
strrchr}}strrchr[28]wcsrchr}}wcsrchr[29] Finds the last occurrence of a byte in a string
strspn}}strspn[30]wcsspn}}wcsspn[31] Returns the number of initial bytes in a string that are in a second string
strcspn}}strcspn[32]wcscspn}}wcscspn[33] Returns the number of initial bytes in a string that are not in a second string
strpbrk}}strpbrk[34]wcspbrk}}wcspbrk[35] Finds in a string the first occurrence of a byte in a set
strstr}}strstr[36]wcsstr}}wcsstr[37] Finds the first occurrence of a substring in a string
strtok}}strtok[38]wcstok}}wcstok[39] Splits a string into tokens
Miscellaneousstrerror}}strerror[40] {{n/a}} Returns a string containing a message derived from an error code
Memory
manipulation
memset}}memset[41]wmemset}}wmemset[42] Fills a buffer with a repeated byte
memcpy}}memcpy[43]wmemcpy}}wmemcpy[44] Copies one buffer to another
memmove}}memmove[45]wmemmove}}wmemmove[46] Copies one buffer to another, possibly overlapping, buffer
memcmp}}memcmp[47]wmemcmp}}wmemcmp[48] Compares two buffers (three-way comparison)
memchr}}memchr[49]wmemchr}}wmemchr[50] Finds the first occurrence of a byte in a buffer
1. ^{{cite web |title=The C99 standard draft + TC3 |url=http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf |accessdate=7 January 2011 |location=§7.1.1p1 }}
2. ^{{cite web |title=The C99 standard draft + TC3 |url=http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf |accessdate=7 January 2011 |location=§6.4.5p7 }}
3. ^{{cite web |title=The C99 standard draft + TC3 |url=http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf |accessdate=7 January 2011 |location=Section 6.4.5 footnote 66}}
4. ^{{cite web |title=The C99 standard draft + TC3 |url=http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf |accessdate=23 December 2011 |location=§5.1.1.2 Translation phases, p1 }}
5. ^{{cite web |url=http://pubs.opengroup.org/onlinepubs/007908775/xsh/stddef.h.html |title=stddef.h - standard type definitions |publisher=The Open Group |accessdate=2017-01-28 }}
6. ^{{cite book |title=Unicode Demystified: A Practical Programmer's Guide to the Encoding Standard |first=Richard |last=Gillam |url=https://books.google.no/books?id=wn5sXG8bEAcC&pg=PA714 |page=714 |publisher=Addison-Wesley Professional |year=2003}}
7. ^For wide string functions substitute wchar_t for "byte" in the description
8. ^{{cite web|url=http://en.cppreference.com/w/c/string/byte/strcpy |title=strcpy - cppreference.com |publisher=En.cppreference.com |date=2014-01-02 |accessdate=2014-03-06}}
9. ^{{cite web|url=http://en.cppreference.com/w/c/string/wide/wcscpy |title=wcscpy - cppreference.com |publisher=En.cppreference.com |accessdate=2014-03-06}}
10. ^{{cite web|url=http://en.cppreference.com/w/c/string/byte/strncpy |title=strncpy - cppreference.com |publisher=En.cppreference.com |date=2013-10-04 |accessdate=2014-03-06}}
11. ^{{cite web|url=http://en.cppreference.com/w/c/string/wide/wcsncpy |title=wcsncpy - cppreference.com |publisher=En.cppreference.com |accessdate=2014-03-06}}
12. ^{{cite web|url=http://en.cppreference.com/w/c/string/byte/strcat |title=strcat - cppreference.com |publisher=En.cppreference.com |date=2013-10-08 |accessdate=2014-03-06}}
13. ^{{cite web|url=http://en.cppreference.com/w/c/string/wide/wcscat |title=wcscat - cppreference.com |publisher=En.cppreference.com |accessdate=2014-03-06}}
14. ^{{cite web|url=http://en.cppreference.com/w/c/string/byte/strncat |title=strncat - cppreference.com |publisher=En.cppreference.com |date=2013-07-01 |accessdate=2014-03-06}}
15. ^{{cite web|url=http://en.cppreference.com/w/c/string/wide/wcsncat |title=wcsncat - cppreference.com |publisher=En.cppreference.com |accessdate=2014-03-06}}
16. ^{{cite web|url=http://en.cppreference.com/w/c/string/byte/strxfrm |title=strxfrm - cppreference.com |publisher=En.cppreference.com |accessdate=2014-03-06}}
17. ^{{cite web|url=http://en.cppreference.com/w/c/string/wide/wcsxfrm |title=wcsxfrm - cppreference.com |publisher=En.cppreference.com |accessdate=2014-03-06}}
18. ^{{cite web|url=http://en.cppreference.com/w/c/string/byte/strlen |title=strlen - cppreference.com |publisher=En.cppreference.com |date=2013-12-27 |accessdate=2014-03-06}}
19. ^{{cite web|url=http://en.cppreference.com/w/c/string/wide/wcslen |title=wcslen - cppreference.com |publisher=En.cppreference.com |accessdate=2014-03-06}}
20. ^{{cite web|url=http://en.cppreference.com/w/c/string/byte/strcmp |title=strcmp - cppreference.com |publisher=En.cppreference.com |accessdate=2014-03-06}}
21. ^{{cite web|url=http://en.cppreference.com/w/c/string/wide/wcscmp |title=wcscmp - cppreference.com |publisher=En.cppreference.com |accessdate=2014-03-06}}
22. ^{{cite web|url=http://en.cppreference.com/w/c/string/byte/strncmp |title=strncmp - cppreference.com |publisher=En.cppreference.com |accessdate=2014-03-06}}
23. ^{{cite web|url=http://en.cppreference.com/w/c/string/wide/wcsncmp |title=wcsncmp - cppreference.com |publisher=En.cppreference.com |accessdate=2014-03-06}}
24. ^{{cite web|url=http://en.cppreference.com/w/c/string/byte/strcoll |title=strcoll - cppreference.com |publisher=En.cppreference.com |accessdate=2014-03-06}}
25. ^{{cite web|url=http://en.cppreference.com/w/c/string/wide/wcscoll |title=wcscoll - cppreference.com |publisher=En.cppreference.com |accessdate=2014-03-06}}
26. ^{{cite web|url=http://en.cppreference.com/w/c/string/byte/strchr |title=strchr - cppreference.com |publisher=En.cppreference.com |date=2014-02-23 |accessdate=2014-03-06}}
27. ^{{cite web|url=http://en.cppreference.com/w/c/string/wide/wcschr |title=wcschr - cppreference.com |publisher=En.cppreference.com |accessdate=2014-03-06}}
28. ^{{cite web|url=http://en.cppreference.com/w/c/string/byte/strrchr |title=strrchr - cppreference.com |publisher=En.cppreference.com |accessdate=2014-03-06}}
29. ^{{cite web|url=http://en.cppreference.com/w/c/string/wide/wcsrchr |title=wcsrchr - cppreference.com |publisher=En.cppreference.com |accessdate=2014-03-06}}
30. ^{{cite web|url=http://en.cppreference.com/w/c/string/byte/strspn |title=strspn - cppreference.com |publisher=En.cppreference.com |accessdate=2014-03-06}}
31. ^{{cite web|url=http://en.cppreference.com/w/c/string/wide/wcsspn |title=wcsspn - cppreference.com |publisher=En.cppreference.com |accessdate=2014-03-06}}
32. ^{{cite web|url=http://en.cppreference.com/w/c/string/byte/strcspn |title=strcspn - cppreference.com |publisher=En.cppreference.com |date=2013-05-31 |accessdate=2014-03-06}}
33. ^{{cite web|url=http://en.cppreference.com/w/c/string/wide/wcscspn |title=wcscspn - cppreference.com |publisher=En.cppreference.com |accessdate=2014-03-06}}
34. ^{{cite web|url=http://en.cppreference.com/w/c/string/byte/strpbrk |title=strpbrk - cppreference.com |publisher=En.cppreference.com |date=2013-05-31 |accessdate=2014-03-06}}
35. ^{{cite web|url=http://en.cppreference.com/w/c/string/wide/wcspbrk |title=wcspbrk - cppreference.com |publisher=En.cppreference.com |accessdate=2014-03-06}}
36. ^{{cite web|url=http://en.cppreference.com/w/c/string/byte/strstr |title=strstr - cppreference.com |publisher=En.cppreference.com |date=2013-10-16 |accessdate=2014-03-06}}
37. ^{{cite web|url=http://en.cppreference.com/w/c/string/wide/wcsstr |title=wcsstr - cppreference.com |publisher=En.cppreference.com |accessdate=2014-03-06}}
38. ^{{cite web|url=http://en.cppreference.com/w/c/string/byte/strtok |title=strtok - cppreference.com |publisher=En.cppreference.com |date=2013-09-03 |accessdate=2014-03-06}}
39. ^{{cite web|url=http://en.cppreference.com/w/c/string/wide/wcstok |title=wcstok - cppreference.com |publisher=En.cppreference.com |accessdate=2014-03-06}}
40. ^{{cite web|url=http://en.cppreference.com/w/c/string/byte/strerror |title=strerror - cppreference.com |publisher=En.cppreference.com |date=2013-05-31 |accessdate=2014-03-06}}
41. ^{{cite web|url=http://en.cppreference.com/w/c/string/byte/memset |title=memset - cppreference.com |publisher=En.cppreference.com |accessdate=2014-03-06}}
42. ^{{cite web|url=http://en.cppreference.com/w/c/string/wide/wmemset |title=wmemset - cppreference.com |publisher=En.cppreference.com |accessdate=2014-03-06}}
43. ^{{cite web|url=http://en.cppreference.com/w/c/string/byte/memcpy |title=memcpy - cppreference.com |publisher=En.cppreference.com |accessdate=2014-03-06}}
44. ^{{cite web|url=http://en.cppreference.com/w/c/string/wide/wmemcpy |title=wmemcpy - cppreference.com |publisher=En.cppreference.com |accessdate=2014-03-06}}
45. ^{{cite web|url=http://en.cppreference.com/w/c/string/byte/memmove |title=memmove - cppreference.com |publisher=En.cppreference.com |date=2014-01-25 |accessdate=2014-03-06}}
46. ^{{cite web|url=http://en.cppreference.com/w/c/string/wide/wmemmove |title=wmemmove - cppreference.com |publisher=En.cppreference.com |accessdate=2014-03-06}}
47. ^{{cite web|url=http://en.cppreference.com/w/c/string/byte/memcmp |title=memcmp - cppreference.com |publisher=En.cppreference.com |accessdate=2014-03-06}}
48. ^{{cite web|url=http://en.cppreference.com/w/c/string/wide/wmemcmp |title=wmemcmp - cppreference.com |publisher=En.cppreference.com |accessdate=2014-03-06}}
49. ^{{cite web|url=http://en.cppreference.com/w/c/string/byte/memchr |title=memchr - cppreference.com |publisher=En.cppreference.com |accessdate=2014-03-06}}
50. ^{{cite web|url=http://en.cppreference.com/w/c/string/wide/wmemchr |title=wmemchr - cppreference.com |publisher=En.cppreference.com |accessdate=2014-03-06}}
51. ^{{cite web|url=http://en.cppreference.com/w/c/string/multibyte/mblen |title=mblen - cppreference.com |publisher=En.cppreference.com |accessdate=2014-03-06}}
52. ^{{cite web|url=http://en.cppreference.com/w/c/string/multibyte/mbtowc |title=mbtowc - cppreference.com |publisher=En.cppreference.com |accessdate=2014-03-06}}
53. ^{{cite web|url=http://en.cppreference.com/w/c/string/multibyte/wctomb |title=wctomb - cppreference.com |publisher=En.cppreference.com |date=2014-02-04 |accessdate=2014-03-06}}
54. ^{{cite web|url=http://en.cppreference.com/w/c/string/multibyte/mbstowcs |title=mbstowcs - cppreference.com |publisher=En.cppreference.com |accessdate=2014-03-06}}
55. ^{{cite web|url=http://en.cppreference.com/w/c/string/multibyte/wcstombs |title=wcstombs - cppreference.com |publisher=En.cppreference.com |accessdate=2014-03-06}}
56. ^{{cite web|url=http://en.cppreference.com/w/c/string/multibyte/btowc |title=btowc - cppreference.com |publisher=En.cppreference.com |accessdate=2014-03-06}}
57. ^{{cite web|url=http://en.cppreference.com/w/c/string/multibyte/wctob |title=wctob - cppreference.com |publisher=En.cppreference.com |accessdate=2014-03-06}}
58. ^{{cite web|url=http://en.cppreference.com/w/c/string/multibyte/mbsinit |title=mbsinit - cppreference.com |publisher=En.cppreference.com |accessdate=2014-03-06}}
59. ^{{cite web|url=http://en.cppreference.com/w/c/string/multibyte/mbrlen |title=mbrlen - cppreference.com |publisher=En.cppreference.com |accessdate=2014-03-06}}
60. ^{{cite web|url=http://en.cppreference.com/w/c/string/multibyte/mbrtowc |title=mbrtowc - cppreference.com |publisher=En.cppreference.com |accessdate=2014-03-06}}
61. ^{{cite web|url=http://en.cppreference.com/w/c/string/multibyte/wcrtomb |title=wcrtomb - cppreference.com |publisher=En.cppreference.com |accessdate=2014-03-06}}
62. ^{{cite web|url=http://en.cppreference.com/w/c/string/multibyte/mbsrtowcs |title=mbsrtowcs - cppreference.com |publisher=En.cppreference.com |accessdate=2014-03-06}}
63. ^{{cite web|url=http://en.cppreference.com/w/c/string/multibyte/wcsrtombs |title=wcsrtombs - cppreference.com |publisher=En.cppreference.com |accessdate=2014-03-06}}
64. ^{{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 |accessdate=2017-01-31 | website="The GNU C Library"}}
65. ^{{cite web |url=http://git.musl-libc.org/cgit/musl/tree/src/multibyte/c16rtomb.c |title=root/src/multibyte/c16rtomb.c |accessdate=2017-01-31 }}
66. ^{{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 |accessdate=2017-01-31 }}
67. ^Here string refers either to byte string or wide string
68. ^{{cite web|url=http://en.cppreference.com/w/c/string/byte/atof |title=atof - cppreference.com |publisher=En.cppreference.com |date=2013-05-31 |accessdate=2014-03-06}}
69. ^{{cite web|url=http://en.cppreference.com/w/c/string/byte/atoi |title=atoi, atol, atoll - cppreference.com |publisher=En.cppreference.com |date=2014-01-18 |accessdate=2014-03-06}}
70. ^{{cite web|url=http://en.cppreference.com/w/c/string/byte/strtof |title=strtof, strtod, strtold - cppreference.com |publisher=En.cppreference.com |date=2014-02-04 |accessdate=2014-03-06}}
71. ^{{cite web|url=http://en.cppreference.com/w/c/string/byte/strtod |title=strtof, strtod, strtold - cppreference.com |publisher=En.cppreference.com |date=2014-02-04 |accessdate=2014-03-06}}
72. ^{{cite web|url=http://en.cppreference.com/w/c/string/byte/strtold |title=strtof, strtod, strtold - cppreference.com |publisher=En.cppreference.com |date=2014-02-04 |accessdate=2014-03-06}}
73. ^{{cite web|url=http://en.cppreference.com/w/c/string/wide/wcstof |title=wcstof, wcstod, wcstold - cppreference.com |publisher=En.cppreference.com |accessdate=2014-03-06}}
74. ^{{cite web|url=http://en.cppreference.com/w/c/string/wide/wcstod |title=wcstof, wcstod, wcstold - cppreference.com |publisher=En.cppreference.com |accessdate=2014-03-06}}
75. ^{{cite web|url=http://en.cppreference.com/w/c/string/wide/wcstold |title=wcstof, wcstod, wcstold - cppreference.com |publisher=En.cppreference.com |accessdate=2014-03-06}}
76. ^{{cite web|url=http://en.cppreference.com/w/c/string/byte/strtol |title=strtol, strtoll - cppreference.com |publisher=En.cppreference.com |date=2014-02-04 |accessdate=2014-03-06}}
77. ^{{cite web|url=http://en.cppreference.com/w/c/string/wide/wcstol |title=wcstol, wcstoll - cppreference.com |publisher=En.cppreference.com |accessdate=2014-03-06}}
78. ^{{cite web|url=http://en.cppreference.com/w/c/string/byte/strtoul |title=strtoul, strtoull - cppreference.com |publisher=En.cppreference.com |date=2014-02-04 |accessdate=2014-03-06}}
79. ^{{cite web|url=http://en.cppreference.com/w/c/string/wide/wcstoul |title=wcstoul, wcstoull - cppreference.com |publisher=En.cppreference.com |accessdate=2014-03-06}}
80. ^C99 Rationale, 7.20.1.1
81. ^{{cite web|url=http://pubs.opengroup.org/onlinepubs/009695399/functions/bzero.html |title=bzero |publisher=The Open Group |accessdate=2017-11-27}}
82. ^{{cite web|url=https://man.openbsd.org/explicit_bzero.3 |title=bzero(3) |publisher=OpenBSD |accessdate=2017-11-27}}
83. ^{{cite web|url=http://pubs.opengroup.org/onlinepubs/009695399/functions/memccpy.html |title=memccpy |publisher=Pubs.opengroup.org |accessdate=2014-03-06}}
84. ^{{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 |accessdate=2014-03-06}}
85. ^{{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 |accessdate=2014-03-06}}
86. ^{{cite web|url=http://msdn.microsoft.com/en-us/library/d45bbxx4%28v=vs.80%29.aspx |title=strcat_s, wcscat_s, _mbscat_s |publisher=Msdn.microsoft.com |accessdate=2014-03-06}}
87. ^{{cite web|url=http://msdn.microsoft.com/en-us/library/td1esda9%28v=vs.80%29.aspx |title=strcpy_s, wcscpy_s, _mbscpy_s |publisher=Msdn.microsoft.com |accessdate=2014-03-06}}
88. ^{{cite web|url=http://pubs.opengroup.org/onlinepubs/009695399/functions/strdup.html |title=strdup |publisher=Pubs.opengroup.org |accessdate=2014-03-06}}
89. ^{{cite web|url=https://www.kernel.org/doc/man-pages/online/pages/man3/strerror.3.html |title=strerror(3) - Linux manual page |publisher=Kernel.org |accessdate=2014-03-06}}
90. ^{{cite web|url=http://www.cprogrammingexpert.com/C/Tutorial/strings/stricmp.aspx |title=String | stricmp() |publisher=C Programming Expert.com |accessdate=2014-03-06}}
91. ^{{cite web|url=http://man.openbsd.org/OpenBSD-5.9/man3/strlcat.3 |title= strlcpy, strlcat — size-bounded string copying and concatenation |publisher=OpenBSD |accessdate=2016-05-26}}
92. ^{{cite web | url=https://www.sudo.ws/todd/papers/strlcpy.html | title=strlcpy and strlcat – consistent, safe, string copy and concatenation. | author=Todd C. Miller |author2=Theo de Raadt | year=1999 | publisher=USENIX '99 }}
93. ^{{cite web|url=http://pubs.opengroup.org/onlinepubs/9699919799/functions/strsignal.html |title=strsignal |publisher=Pubs.opengroup.org |accessdate=2014-03-06}}
94. ^{{cite web|url=http://pubs.opengroup.org/onlinepubs/009695399/functions/strtok.html |title=strtok |publisher=Pubs.opengroup.org |accessdate=2014-03-06}}
95. ^{{cite web |url=https://www.openbsd.org/papers/portability.pdf |title=Secure Portability |first1=Damien |last1=Miller |date=October 2005 |format=PDF |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.}}
96. ^libc-alpha mailing list, selected messages from 8 August 2000 thread: 53, 60, 61
97. ^[https://lwn.net/Articles/507319/ The ups and downs of strlcpy(); LWN.net]
98. ^{{cite web |url=http://git.musl-libc.org/cgit/musl/tree/src/string/strlcpy.c |title=root/src/string/strlcpy.c |accessdate=2017-01-28 }}
99. ^{{cite web |url=http://git.musl-libc.org/cgit/musl/tree/src/string/strlcat.c |title=root/src/string/strlcat.c |accessdate=2017-01-28 }}
100. ^{{cite web|url=http://bxr.su/OpenBSD/lib/libc/string/strlcpy.c |title= strlcpy.c |author=Todd C. Miller |website=BSD Cross Reference}}
101. ^{{cite web|url=http://bxr.su/OpenBSD/lib/libc/string/strlcat.c |title= strlcat.c |author=Todd C. Miller |website=BSD Cross Reference}}
102. ^{{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|accessdate=13 February 2015}}
103. ^{{cite web |title=Field Experience With Annex K — Bounds Checking Interfaces |url=http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1967.htm |accessdate=5 November 2015}}
104. ^{{cite web |title=The C11 standard draft |url=http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1548.pdf |accessdate=13 February 2013 |location=§K.3.1.4p2}}
105. ^{{cite web |title=The C11 standard draft |url=http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1548.pdf |accessdate=13 February 2013 |location=§K.3.6.1.1p4}}
106. ^{{cite web|title=Parameter Validation|url=https://msdn.microsoft.com/en-us/library/ksazx244.aspx}}
107. ^{{cite web |title=The C11 standard draft |url=http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1548.pdf |accessdate=13 February 2013 |location=§K.3.7.2.1p4}}
108. ^{{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 | accessdate=10 November 2011 }}
109. ^{{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 | accessdate=6 March 2013 }}

Multibyte functions

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

These functions all take a pointer to a mbstate_t object that 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.[64][65][66]

{{Clear}}

{{anchor|stdlib.h}}Numeric conversions

Byte
string
Wide
string
Description[67]
atof}}atof[68] {{n/a}} converts a string to a floating-point value
atoi|atol|atoll}}atoi
atol
atoll[69]
{{n/a}} converts a string to an integer (C99)
strtof|strtod|strtold}}strtof (C99)[70]
strtod[71]
strtold (C99)[72]
wcstof|wcstod|wcstold}}wcstof (C99)[73]
wcstod[74]
wcstold (C99)[75]
converts a string to a floating-point value
strtol|strtoll}}strtol
strtoll[76]
wcstol|wcstoll}}wcstol
wcstoll[77]
converts a string to a signed integer
strtoul|strtoull}}strtoul
strtoull[78]
wcstoul|wcstoull}}wcstoul
wcstoull[79]
converts a string to an unsigned integer

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.[80]{{clear}}

Popular extensions

Name Platform Description
bzero}}bzero[81][82]POSIX, BSDFills a buffer with zero bytes, deprecated by memset
memccpy}}memccpy[83]SVID, POSIXcopies up to specified number of bytes between two memory areas, which must not overlap, stopping when a given byte is found.
mempcpy}}mempcpy[84]GNUa variant of memcpy returning a pointer to the byte following the last written byte
strcasecmp}}strcasecmp[85]POSIX, BSDcase-insensitive versions of strcmp
strcat_s}}strcat_s[86]Windowsa variant of strcat that checks the destination buffer size before copying
strcpy_s}}strcpy_s[87]Windowsa variant of strcpy that checks the destination buffer size before copying
strdup}}strdup[88]POSIXallocates and duplicates a string
strerror_r}}strerror_r[89]POSIX 1, GNUa variant of strerror that is thread-safe. The GNU version is incompatible with the POSIX one.
stricmp}}stricmp[90]Windowscase-insensitive versions of strcmp
strlcpy}}strlcpy[91]BSD, Solarisa variant of strcpy that truncates the result to fit in the destination buffer[92]
strlcat}}strlcat[91]BSD, Solarisa variant of strcat that truncates the result to fit in the destination buffer[92]
strsignal}}strsignal[93]POSIX:2008returns string representation of a signal code. Not thread safe.
strtok_r}}strtok_r[94]POSIXa variant of strtok that is thread-safe

Replacements

Despite the well-established need to replace strcat[12] and strcpy[8] 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.[92]

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 and strlcpy functions, which appeared in OpenBSD 2.4 in December, 1998.[92] 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[95] and encouraging the use of C strings (instead of some superior alternative form of string).[96][97] 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 musl introduced in 2011.[98][99] 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.[100][101]

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).[102] These functions were standardized with some minor changes as part of the optional C11 (Annex K) proposed by ISO/IEC WDTR 24731. 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.[103] 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,[104] which usually aborts the program.[105][106] Some functions perform destructive operations before calling the runtime-constraint handler; for example, strcat_s sets the destination to the empty string,[107] 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.[108] Although open-source implementations of these functions are available,[109] these functions are not present in common Unix C libraries.

If the string length is known, then memcpy[43] or memmove[45] can be more efficient than strcpy{{Citation needed|date=December 2015}}, so some programs{{Who|date=December 2015}} 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

  • {{Section link|C syntax|Strings}}{{snd}} source code syntax, including backslash escape sequences
  • String functions

Notes

{{Notelist}}

References

External links

{{wikibooks|C Programming|Strings|C Programming/Strings}}
  • Fast memcpy in C, multiple C coding examples to target different types of CPU instruction architectures
{{CProLang|state=expanded}}

3 : C (programming language)|C standard library|String (computer science)

随便看

 

开放百科全书收录14589846条英语、德语、日语等多语种百科知识,基本涵盖了大多数领域的百科知识,是一部内容自由、开放的电子版国际百科全书。

 

Copyright © 2023 OENC.NET All Rights Reserved
京ICP备2021023879号 更新时间:2024/9/21 19:00:59