C wchar.h Functions

Beginner
12 min read
Updated: Nov 2026
59 tutorials
<wchar.h>

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.

Need the model first?

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

JobStart with
Measure a wide stringwcslen()
Copy or append wide textwcscpy(), wcscat()
Compare or search stringswcscmp(), wcsstr()
Print or scan wide textwprintf(), wscanf()
Convert multibyte textmbrtowc(), wcrtomb()
Parse a numberwcstod(), wcstol()
Process a counted wchar_t blockwmemmove(), wmemset()

Jump to a category

How to Browse This Index

  1. Start with the wide-character lesson if wchar_t, wide literals, or locale-dependent conversion is new.
  2. Use Choose a Function by Job when you know the task but not the API name.
  3. Type a name or keyword into the search box to filter all categories immediately.
  4. Open one function tutorial for its exact parameter contract, return sentinels, and examples.
  5. 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 functions

Convert characters and strings between multibyte encodings and wchar_t data while tracking conversion state.

FunctionDescriptionTutorial
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 functions

Read and write wide characters and strings through FILE streams, standard input, and standard output.

FunctionDescriptionTutorial
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 functions

Print, scan, and buffer formatted wide text, including va_list variants for reusable wrappers.

FunctionDescriptionTutorial
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 functions

Measure, copy, append, compare, search, tokenize, transform, and format null-terminated wide strings.

FunctionDescriptionTutorial
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 functions

Parse floating-point and integer values from wide strings and report where parsing stopped.

FunctionDescriptionTutorial
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 functions

Search, compare, copy, move, and fill counted blocks of wchar_t elements.

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

FamilyData 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 / ...wcharWide-oriented stream I/O. Store character results in wint_t when WEOF must also fit.
...printfFormatted output. Match every format conversion to the promoted argument type and check the written count.
...scanfFormatted 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' and L"Hello"; wide strings end with L'\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

wchar.h declares wide-character types, conversion helpers, wide I/O, string operations, and memory functions.
wchar_t is an integer type used to store wide characters.
Prefix the literal with L, for example L"Hello".
Use wprintf(L"%ls ", text) or another wide formatted-output function.
Start with wcslen(), wprintf(), wcscpy(), and wcscmp(), then explore conversion functions.

Start with wcslen()

Measure a wide string and learn the core data model.

wcslen() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

59 people found this page helpful