C wchar_t and 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".
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
| Feature | char text | wchar_t text |
|---|---|---|
| Character literal | 'A' | L'A' |
| String literal | "Hello" | L"Hello" |
| String element | char | wchar_t |
| Null terminator | '\0' | L'\0' |
| Common print conversion | %s | %ls with wprintf |
| Length function | strlen() | 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().
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.
| Value | Call |
|---|---|
| One wide character | wprintf(L"%lc\n", letter); |
| A wide string | wprintf(L"%ls\n", text); |
| A size_t length | wprintf(L"%zu\n", length); |
| An integer | wprintf(L"%d\n", number); |
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.
#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
- Adopt the environment locale.
setlocale()lets wide I/O use the locale configured for the process. - Create wide data. The
Lprefix makesmessagepoint to a wide string literal. - Measure elements.
wcslen()counts the wide characters beforeL'\0'. - Print with matching formats.
%lsreceives the wide string and%zureceives thesize_tlength.
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
Browse wchar.h functions
Choose from conversion, I/O, strings, numbers, formatting, and memory.
10 people found this page helpful
