C wchar.h Header

Beginner
6 min read
Updated: Oct 2026
3 examples
Live preview

What Is wchar.h?

<wchar.h> is the C standard header for wide characters and wide strings. A wide string is an array of wchar_t ending with L'\0'. Use the L prefix for literals (L"Hello"), and APIs that mirror string.h with a wcs name.

Remember
#include <wchar.h>

L"text"  → wchar_t array + L'\0'
wcslen / wcscpy / wcscmp  ≈ strlen / strcpy / strcmp

wprintf(L"%ls", wide);   // wide format + %ls
mbstowcs  char*  → wchar_t*
wcstombs  wchar_t* → char*

setlocale before conversion (locale.h)
classification → wctype.h (iswalpha, …)
UsePrefer
Wide string length / copy / comparewcslen / wcscpy / wcscmp
Print wide textwprintf with %ls
Letter / digit tests on wide charswctype.h

How to Use wchar.h in C

Think of it as string.h for wchar_t, plus conversion helpers.

  1. Include <wchar.h> (and <stdio.h> for wprintf).
  2. Write wide literals with L"..." and size buffers in wchar_t elements.
  3. Use wcslen, wcscpy, wcscat, wcscmp like the narrow twins.
  4. Call setlocale from locale.h before mbstowcs / wcstombs for non-ASCII text.
PieceRequired?Role
#include <wchar.h>YesWide string and conversion APIs
L prefixFor literalsMakes wide characters / strings
setlocaleFor convertDefines multibyte encoding rules

Pseudocode

Pseudocode
wide = L"Hello, World!"
len  = wcslen(wide)

wprintf(L"Text: %ls (len %zu)\n", wide, len)

setlocale(LC_ALL, "")
n = wcstombs(narrow_buf, wide, sizeof(narrow_buf))
if n == (size_t)-1:
    conversion failed
else:
    narrow_buf[n] = '\0'
    printf("%s\n", narrow_buf)

// reverse direction:
mbstowcs(wide_buf, narrow_src, wide_capacity)

Cheat Sheet

Narrow vs wide name map and common calls.

NarrowWide
strlenwcslen
strcpy / strcatwcscpy / wcscat
strcmpwcscmp
strchr / strstrwcschr / wcsstr
printf("%s")wprintf(L"%ls")
—mbstowcs / wcstombs
strftimewcsftime
EOFWEOF

Live Preview

Explore wide-string ideas: length, append, compare, and convert (browser simulation of the C patterns).

Simulates wchar.h operations on L"Hello, World!" (same idea as the C examples below).

Result
wstr = L"Hello, World!"
wcslen(wstr) → 13
(counts wchar_t units before L'\0')

Worked Walkthrough — Wide “Hello, World!”

Trace length, print, and convert to a narrow char buffer.

Calculation
wstr = L"Hello, World!"

Memory idea (elements are wchar_t):
  H e l l o ,   W o r l d !  L'\0'
  1 2 3 4 5 6 7 8 …         13 chars + terminator

wcslen(wstr) → 13

wprintf(L"%ls\n", wstr)
  → Hello, World!

setlocale(LC_ALL, "")
wcstombs(mbs, wstr, sizeof(mbs))
  → narrow "Hello, World!" for printf("%s")

C Examples

Compile with gcc file.c -std=c11 -o out. Wide console output may need a Unicode-capable terminal.

Example 1 — wprintf, wcslen, wcstombs

Print a wide string, measure it, then convert to multibyte for printf.

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

int main(void) {
    wchar_t wstr[] = L"Hello, World!";
    char mbs[64];
    size_t converted;

    setlocale(LC_ALL, "");

    wprintf(L"Wide string: %ls\n", wstr);
    wprintf(L"Length (wcslen): %zu\n", wcslen(wstr));

    converted = wcstombs(mbs, wstr, sizeof(mbs));
    if (converted == (size_t)(-1)) {
        fprintf(stderr, "wcstombs failed\n");
        return 1;
    }
    mbs[converted] = '\0';
    printf("Multibyte string: %s\n", mbs);

    return 0;
}

How It Works

  1. Wide literal. L"..." builds a wchar_t array ended by L'\0'.
  2. Wide print. wprintf needs a wide format; %ls prints a wide string.
  3. Length. wcslen counts wide characters before the terminator (13).
  4. Convert out. After setlocale, wcstombs fills a char buffer for narrow APIs; check for (size_t)-1.

Example 2 — Build a Message with wcscat

Append like strcat, but on wchar_t buffers.

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

int main(void) {
    wchar_t buf[32] = L"Hello, ";
    const wchar_t *name = L"Ada";

    wcscat(buf, name);
    wprintf(L"Greeting: %ls\n", buf);
    wprintf(L"Total length: %zu\n", wcslen(buf));

    return 0;
}

How It Works

  1. Start buffer. buf already holds L"Hello, " plus a terminator.
  2. Append. wcscat finds that terminator and writes L"Ada" after it.
  3. Capacity. buf[32] must fit both parts plus L'\0'—same rule as narrow strcat.
  4. Measure. wcslen reports 10 wide characters. Prefer wcsncat when sizes are tight.

Example 3 — Narrow to Wide with mbstowcs

Convert a locale-encoded char string into wchar_t.

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

int main(void) {
    const char *narrow = "Cafe";
    wchar_t wide[32];
    size_t n;

    setlocale(LC_ALL, "");

    n = mbstowcs(wide, narrow, 32);
    if (n == (size_t)(-1)) {
        fprintf(stderr, "mbstowcs failed\n");
        return 1;
    }
    wide[n] = L'\0';

    wprintf(L"Wide: %ls (%zu chars)\n", wide, wcslen(wide));

    return 0;
}

How It Works

  1. Set locale. Multibyte rules come from setlocale.
  2. Convert. mbstowcs reads the narrow string and writes up to 32 wide characters.
  3. Terminate. Ensure L'\0' after the converted length (some cases need an explicit write).
  4. Use wide APIs. Print with wprintf. Non-ASCII bytes need a matching locale (for example UTF-8 on many Linux systems).

Edge Cases & Pitfalls

Wide APIs fail quietly when formats, locales, or sizes are wrong.

format

Use %ls with wprintf

A narrow "%s" format with a wide string is wrong. Prefer wprintf(L"%ls", w).

locale

setlocale before conversion

Without a proper locale, mbstowcs/wcstombs may fail on non-ASCII text.

size

Buffers are wchar_t units

wchar_t buf[32] holds 32 wide characters, not 32 bytes. Size destinations carefully.

unicode

wchar_t is not portable Unicode

Width and encoding are implementation-defined. Many modern apps prefer UTF-8 in char instead.

Performance Notes

ApproachCostBest for
wcslen / wcscmpO(n) over wide charsShort UI strings
mbstowcs / wcstombsO(n) + locale tablesBoundary with narrow APIs
Stay in one encodingFewer conversionsHot paths (UTF-8 or wide)

Avoid converting the same text back and forth in a loop. Pick wide or narrow for a pipeline and convert only at I/O boundaries.

Key Takeaways

  • Literals: use L"..." for wchar_t strings.
  • Names: wcs* mirrors str* from string.h.
  • Print: wprintf(L"%ls", s) for wide strings.
  • Convert: setlocale then mbstowcs/wcstombs.

One line: wchar.h is string.h for wchar_t—plus locale-aware bridges to narrow char strings.

Frequently Asked Questions

wchar.h is the C standard header for wide characters and wide strings. It provides wchar_t strings (wcslen, wcscpy), wide I/O (wprintf), and conversion between multibyte char strings and wchar_t with mbstowcs and wcstombs.
char is typically one byte per element for narrow strings. wchar_t is an integer type wide enough for one wide character—often 2 or 4 bytes depending on the platform. Wide literals use the L prefix: L"hello".
L before a character or string literal makes it wide. L'A' has type wchar_t; L"text" is an array of wchar_t ending with L'\0'. Use wide literals with wprintf, wcslen, and other wchar.h functions.
printf writes narrow char strings. wprintf writes wide strings and needs a wide format string (use %ls for wchar_t*). Mixing narrow and wide I/O on the same stream without care can garble output.
wchar.h focuses on wide strings, conversion, and wide I/O. wctype.h provides wide character classification and case mapping (iswalpha, towlower). Include both when you need strings and classification.
wchar_t width and encoding are implementation-defined—not portable Unicode. Many new projects use char with UTF-8 instead. wchar.h remains important on Windows APIs and legacy internationalized C code.

Next: wctype.h

Classify and case-map wide characters with isw* and tow* functions.

wctype.h 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.

9 people found this page helpful