Site Tools


wiki:h-wchar

Table of Contents

<wchar.h>

<wchar.h> provides wide-character functions (C95): wprintf, wcslen, wcscmp, etc. The encoding of wchar_t is platform-dependent (UTF-32 on Linux, UTF-16 on Windows), making portable Unicode code difficult.

For portable Unicode, prefer <uchar.h> with char32_t (UTF-32) or char16_t (UTF-16).

Example

This example demonstrates wide-string handling and locale-aware output.

// compile: gcc -o wcharexample wcharexample.c
// run: ./wcharexample
// description: wide string handling and character counting
 
#include <wchar.h>
#include <locale.h>
#include <string.h>
#include <stdio.h>
 
int main() {
    setlocale(LC_ALL, "");
 
    const wchar_t* ws = L"héllo";
    const char* ns = "héllo";
 
    wprintf(L"wide string: %ls\n", ws);
    wprintf(L"wcslen: %zu (wchar units)\n", wcslen(ws));
    printf("strlen: %zu (bytes)\n", strlen(ns));
    printf("sizeof(wchar_t): %zu\n", sizeof(wchar_t));
 
    return 0;
}
wiki/h-wchar.md · Last modified: by 127.0.0.1