Strings
Overview
A C-style string is a contiguous sequence of characters terminated by the NUL character (refer to ASCII). Text data is considered more platform-independent than binary data since it is unaffected by word size or byte ordering.
Strings can embed escape sequences, denoted with a backslash (\), used to represent characters:
\ooo: Consists of one to three octal digits.\xhh: Consists of one or more hexadecimal digits.- The
xprefix is required to distinguish from octal escape sequences.
- The
\uhhhh: Introduced in C11 to represent Unicode code points.- Must have exactly four hexadecimal characters specified with
0leading padding if necessary.
- Must have exactly four hexadecimal characters specified with
\Uhhhhhhhh: Introduced in C11 to represent larger unicode code points.- Must have exactly eight hexadecimal characters specified with
0leading padding if necessary.
- Must have exactly eight hexadecimal characters specified with
Multibyte Strings
A multibyte string is a C-style string composed of multibyte characters. A multibyte character is a character that may require more than one byte to represent.
Multibyte characters are represented with type char. Multibyte strings are represented with type char*.
UTF-8
A UTF-8 string literal is prefixed with u8. Since C23, a UTF-8 character literal is denoted in the same way.
Wide Character Strings
A wide character is a single value that can uniquely represent all code points of the largest extended character set specified among the supported locales. A wide character string is a NUL-terminated array of wide characters.
Wide characters are represented with type wchar_t. Wide character strings are represented with type wchar_t*.
UTF-16
If the __STDC_UTF_16__ macro is set to 1, then char16_t strings are UTF-16 encoded. Character and string literals of type char16_t are denoted with a u prefix. Since C23, this macro must be set to 1.
UTF-32
If the __STDC_UTF_32__ macro is set to 1, then char32_t strings are UTF-32 encoded. Character and string literals of type char32_t are denoted with a U prefix. Since C23, this macro must be set to 1.
Copying
The two primary functions used for copying memory are memcpy and memmove:
void* memcpy(void* restrict s1, const void* restrict s2, size_t n);
void* memmove(void* s1, const void* s2, size_t n);