C wchar.h Functions

Definition and Usage
<wchar.h> is the standard C header for wide-character text. Its functions work with wchar_t, wint_t, conversion state, wide-oriented streams, and null-terminated wide strings.
This page is the catalog for all 59 tutorials. Use it when you know the job but not the function name, or search when you already know a name. Each guide explains the declaration, parameters, return value, pitfalls, and three worked examples.
Wide text is not automatically Unicode
wchar_t is an implementation-defined integer type. Its width and encoding differ across systems, and conversions depend on the active C locale. Treat wide-character APIs as a C text model, not as a promise that one element always equals one user-perceived character.
Read C wchar_t and wchar.h for wide literals, L'\0', %ls, locale basics, and a starter program.
C wchar.h Syntax
Include the header, create wide data with an L prefix, and use the function that matches the job:
#include <stdio.h>
#include <wchar.h>
int main(void) {
wchar_t destination[32];
wcscpy(destination, L"Hello");
wprintf(L"%ls has %zu wide characters\n",
destination, wcslen(destination));
return 0;
}The destination is writable, the source is a wide literal, %ls prints a wide string, and %zu prints the size_t returned by wcslen().
Choose a Function by Job
| Job | Start with |
|---|---|
| Measure a wide string | wcslen() |
| Copy or append wide text | wcscpy(), wcscat() |
| Compare or search strings | wcscmp(), wcsstr() |
| Print or scan wide text | wprintf(), wscanf() |
| Convert multibyte text | mbrtowc(), wcrtomb() |
| Parse a number | wcstod(), wcstol() |
| Process a counted wchar_t block | wmemmove(), wmemset() |
Jump to a category
How to Browse This Index
- Start with the wide-character lesson if
wchar_t, wide literals, or locale-dependent conversion is new. - Use Choose a Function by Job when you know the task but not the API name.
- Type a name or keyword into the search box to filter all categories immediately.
- Open one function tutorial for its exact parameter contract, return sentinels, and examples.
- Follow previous and next links to continue through the stable 59-function sequence.
C wchar.h Functions Index
Search all 59 standard wide-character tutorials by function name or description.
Character conversion
8 functionsConvert characters and strings between multibyte encodings and wchar_t data while tracking conversion state.
| Function | Description | Tutorial |
|---|---|---|
btowc() | Converts one single-byte character to a wide character. | Open |
wctob() | Converts a wide character to a single-byte character. | Open |
mbrlen() | Measures the next multibyte character. | Open |
mbrtowc() | Converts the next multibyte character to wchar_t. | Open |
wcrtomb() | Converts one wide character to a multibyte sequence. | Open |
mbsinit() | Tests whether a conversion state is initial. | Open |
mbsrtowcs() | Converts a multibyte string to a wide string. | Open |
wcsrtombs() | Converts a wide string to a multibyte string. | Open |
Wide I/O
10 functionsRead and write wide characters and strings through FILE streams, standard input, and standard output.
| Function | Description | Tutorial |
|---|---|---|
fgetwc() | Reads one wide character from a stream. | Open |
fgetws() | Reads a line of wide characters from a stream. | Open |
fputwc() | Writes one wide character to a stream. | Open |
fputws() | Writes a wide string to a stream. | Open |
fwide() | Checks or sets a stream's byte or wide orientation. | Open |
getwc() | Reads one wide character from a stream. | Open |
getwchar() | Reads one wide character from standard input. | Open |
putwc() | Writes one wide character to a stream. | Open |
putwchar() | Writes one wide character to standard output. | Open |
ungetwc() | Pushes one wide character back onto a stream. | Open |
Formatted print/scan
12 functionsPrint, scan, and buffer formatted wide text, including va_list variants for reusable wrappers.
| Function | Description | Tutorial |
|---|---|---|
wprintf() | Prints formatted wide text to standard output. | Open |
wscanf() | Reads formatted wide input from standard input. | Open |
fwprintf() | Prints formatted wide text to a stream. | Open |
fwscanf() | Reads formatted wide input from a stream. | Open |
swprintf() | Formats wide text into a buffer. | Open |
swscanf() | Parses formatted data from a wide string. | Open |
vfwprintf() | Prints a va_list as wide text to a stream. | Open |
vfwscanf() | Reads formatted wide input from a stream using va_list. | Open |
vswprintf() | Formats a va_list into a wide buffer. | Open |
vswscanf() | Parses a wide string using a va_list. | Open |
vwprintf() | Prints a va_list as wide text to standard output. | Open |
vwscanf() | Reads standard input using a va_list. | Open |
Wide string ops
17 functionsMeasure, copy, append, compare, search, tokenize, transform, and format null-terminated wide strings.
| Function | Description | Tutorial |
|---|---|---|
wcslen() | Counts wide characters before the null terminator. | Open |
wcscpy() | Copies a wide string. | Open |
wcsncpy() | Copies up to a fixed number of wide characters. | Open |
wcscat() | Appends one wide string to another. | Open |
wcsncat() | Appends at most a fixed number of wide characters. | Open |
wcscmp() | Compares two wide strings. | Open |
wcsncmp() | Compares the first part of two wide strings. | Open |
wcscoll() | Compares wide strings using the current locale. | Open |
wcsxfrm() | Builds a locale collation key for a wide string. | Open |
wcschr() | Finds the first matching wide character. | Open |
wcsrchr() | Finds the last matching wide character. | Open |
wcsstr() | Finds a wide substring. | Open |
wcspbrk() | Finds the first character from a set. | Open |
wcsspn() | Counts an initial span containing only accepted characters. | Open |
wcscspn() | Counts characters before any rejected character. | Open |
wcstok() | Splits a writable wide string into tokens. | Open |
wcsftime() | Formats date and time as a wide string. | Open |
Conversion from wide
7 functionsParse floating-point and integer values from wide strings and report where parsing stopped.
| Function | Description | Tutorial |
|---|---|---|
wcstod() | Converts wide text to double. | Open |
wcstof() | Converts wide text to float. | Open |
wcstold() | Converts wide text to long double. | Open |
wcstol() | Converts wide text to long. | Open |
wcstoll() | Converts wide text to long long. | Open |
wcstoul() | Converts wide text to unsigned long. | Open |
wcstoull() | Converts wide text to unsigned long long. | Open |
Wide memory
5 functionsSearch, compare, copy, move, and fill counted blocks of wchar_t elements.
| Function | Description | Tutorial |
|---|---|---|
wmemchr() | Finds a wide character in a counted block. | Open |
wmemcmp() | Compares counted wide-character blocks. | Open |
wmemcpy() | Copies a non-overlapping wide-character block. | Open |
wmemmove() | Copies a wide-character block safely when regions overlap. | Open |
wmemset() | Fills a wide-character block with one value. | Open |
How the Function Families Differ
Similar names do not always imply identical contracts. Use the prefix and parameter types to identify what kind of data a function expects.
| Family | Data model and main check |
|---|---|
wcs... | Usually null-terminated wide strings. Verify termination, destination capacity, and returned pointers or lengths. |
wmem... | Exactly a counted number of wchar_t elements. Embedded nulls are allowed, and no terminator is added automatically. |
mbr... / wcr... | Stateful multibyte conversion. Initialize mbstate_t and handle invalid or incomplete sequence sentinels. |
f...wc / ...wchar | Wide-oriented stream I/O. Store character results in wint_t when WEOF must also fit. |
...printf | Formatted output. Match every format conversion to the promoted argument type and check the written count. |
...scanf | Formatted input. Pass correctly typed destination pointers and check the assignment count before reading values. |
wcsto... | Numeric parsing. Inspect endptr for consumed input and errno for range errors. |
The individual tutorials turn these family rules into concrete examples and explain exceptions for each API.
Key Takeaways
- Wide literals use L. Write
L'A'andL"Hello"; wide strings end withL'\0'. - Use the right units. Most counts are wchar_t elements, while multibyte conversion results may count bytes.
- Respect stream orientation. Do not casually mix byte and wide I/O on one stream.
- Check sentinels. WEOF, NULL, negative statuses, and converted size_t sentinel values carry different meanings.
- Locale matters. Conversion and collation behavior can change with the active C locale.
Frequently Asked Questions
Start with wcslen()
Measure a wide string and learn the core data model.
59 people found this page helpful
