WCHAR
WCHAR is a Windows data type that represents a 16‑bit Unicode character. It is used throughout the Windows API to handle text in the Unicode format.
Syntax
typedef wchar_t WCHAR;
Remarks
- Size: 2 bytes (16 bits) on all Windows platforms.
- Encoding: UTF‑16 code unit. Surrogate pairs may be required for characters outside the Basic Multilingual Plane.
- Functions that accept
WCHAR*
generally expect a null‑terminated string (wide string). - When interfacing with legacy APIs that expect
char
, you must convert between ANSI and Unicode usingMultiByteToWideChar
orWideCharToMultiByte
.
Related Types
- CHAR – 8‑bit ANSI character.
- LPWSTR – Pointer to a
WCHAR
string. - LPCWSTR – Constant pointer to a
WCHAR
string.
Example
Printing a WCHAR
string using wprintf
:
#include <stdio.h>
#include <windows.h>
int wmain(void)
{
WCHAR greeting[] = L"Hello, 世界!";
wprintf(L"%s\n", greeting);
return 0;
}