C wchar_t and wchar.h

Beginner
8 min read
Updated: Nov 2026
1 example
<wchar.h>

Wide Characters in C

wchar_t is C's wide-character type. It gives the implementation a type for a wider character set than ordinary char text may directly represent. The <wchar.h> header supplies functions for wide strings, formatted I/O, stream I/O, conversion, number parsing, and counted memory blocks.

A wide string is an array of wchar_t values ending with the wide null character L'\0'. Write a wide string literal by placing L before the quotes: L"Hello".

Key idea.

wchar_t is a C storage and library model. It does not guarantee that one element equals one visible character, and its width is not the same on every platform.

wchar_t vs char

Featurechar textwchar_t text
Character literal'A'L'A'
String literal"Hello"L"Hello"
String elementcharwchar_t
Null terminator'\0'L'\0'
Common print conversion%s%ls with wprintf
Length functionstrlen()wcslen()

Do not pass a char * to a function expecting wchar_t *, or cast between them to silence a warning. They have different element types and representations. Convert through the multibyte conversion APIs when data crosses that boundary.

L"" Literals and L'\0'

L"cat" creates an array containing L'c', L'a', L't', and L'\0'. The ending null is part of storage but is not included in the value returned by wcslen().

C
wchar_t editable[] = L"cat";
const wchar_t *read_only = L"dog";

editable[0] = L'C';       /* writable array */
/* read_only[0] = L'D'; */ /* do not modify a literal */

Use an array when a function such as wcscpy() or wcstok() must modify text. Keep pointers to literals const.

Printing Wide Text with %ls

Wide formatted I/O uses a wide format literal. With wprintf(), %lc prints one wide character and %ls prints a null-terminated wide string.

ValueCall
One wide characterwprintf(L"%lc\n", letter);
A wide stringwprintf(L"%ls\n", text);
A size_t lengthwprintf(L"%zu\n", length);
An integerwprintf(L"%d\n", number);
Keep stream orientation consistent.

Once a stream becomes wide-oriented, continue with wide I/O on that stream. Mixing printf() and wprintf() on the same stream is not a portable output strategy.

Why Locale Matters

C starts in the "C" locale. Multibyte-to-wide conversion, wide-to-multibyte conversion, and locale-aware collation interpret data according to the active locale. A program that wants the user's configured environment commonly calls setlocale(LC_ALL, "").

#include <locale.h>

if (setlocale(LC_ALL, "") == NULL) {
    /* The requested environment locale was unavailable. */
}

Setting a locale does not repair incorrectly encoded input. Conversion functions still need return-value checks for invalid or incomplete byte sequences.

Starter wchar_t Program

This complete example creates a wide string, measures it, and prints it with its length.

C
#include <locale.h>
#include <stdio.h>
#include <wchar.h>

int main(void) {
    setlocale(LC_ALL, "");

    const wchar_t *message = L"Hello, wide C!";
    size_t length = wcslen(message);

    wprintf(L"%ls\n", message);
    wprintf(L"wide characters = %zu\n", length);
    return 0;
}

How It Works

  1. Adopt the environment locale. setlocale() lets wide I/O use the locale configured for the process.
  2. Create wide data. The L prefix makes message point to a wide string literal.
  3. Measure elements. wcslen() counts the wide characters before L'\0'.
  4. Print with matching formats. %ls receives the wide string and %zu receives the size_t length.

Four Functions to Learn First

When those feel familiar, open the complete wchar.h function index and choose conversion, I/O, formatting, string, number, or memory APIs by job.

Frequently Asked Questions

wchar_t is an implementation-defined integer type capable of representing the implementation's wide characters. Include <wchar.h> for wide-character library functions.
Prefix the literal with L, for example L"Hello". Its elements have type wchar_t and it ends with L'\0'.
Use a wide output function such as wprintf with the %ls conversion: wprintf(L"%ls\n", text). Use %lc for one wide character.
No. Its width and encoding are implementation-defined. Windows commonly uses 16-bit wchar_t, while many Unix-like systems use 32-bit wchar_t.
Multibyte conversions and character handling use the active C locale. Call setlocale when the program needs the user's configured locale, then check conversion errors.

Browse wchar.h functions

Choose from conversion, I/O, strings, numbers, formatting, and memory.

Function index →

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.

10 people found this page helpful