<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.
Think of it as string.h for wchar_t, plus conversion helpers.
Include <wchar.h> (and <stdio.h> for wprintf).
Write wide literals with L"..." and size buffers in wchar_t elements.
Use wcslen, wcscpy, wcscat, wcscmp like the narrow twins.
Call setlocale from locale.h before mbstowcs / wcstombs for non-ASCII text.
Piece
Required?
Role
#include <wchar.h>
Yes
Wide string and conversion APIs
L prefix
For literals
Makes wide characters / strings
setlocale
For convert
Defines 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)
Reference
Cheat Sheet
Narrow vs wide name map and common calls.
Narrow
Wide
strlen
wcslen
strcpy / strcat
wcscpy / wcscat
strcmp
wcscmp
strchr / strstr
wcschr / wcsstr
printf("%s")
wprintf(L"%ls")
—
mbstowcs / wcstombs
strftime
wcsftime
EOF
WEOF
Try it
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')
Trace
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")
Code
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.
Width and encoding are implementation-defined. Many modern apps prefer UTF-8 in char instead.
Analysis
Performance Notes
Approach
Cost
Best for
wcslen / wcscmp
O(n) over wide chars
Short UI strings
mbstowcs / wcstombs
O(n) + locale tables
Boundary with narrow APIs
Stay in one encoding
Fewer conversions
Hot 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.
Remember
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.