Wide Characters & Unicode Realities in C
char holds bytes, not characters, in a UTF-8 world — and C's wide-character support (wchar_t, <wchar.h>, mbsrtowcs conversions) bridges the gap where the platform demands it (Windows APIs, locale-aware collation). Wide strings (L"...") with wprintf/wcslen mirror the narrow API with w-prefixed names.
The honest modern guidance: store and exchange UTF-8 in plain char strings (it sorts, prints, and transmits fine as bytes), validate with explicit UTF-8 routines, and reach for wide characters only at APIs that require them. Most C UTF-8 work is careful byte handling, not wide conversion.
Bytes vs characters
strlen counts bytes — a 3-byte UTF-8 character counts 3. Length in characters needs decoding, not strlen.
Wide API mirrors narrow
wchar_t strings with wprintf, wcslen, wcscmp — same shapes, wider units, locale-dependent widths.
Convert at boundaries
mbsrtowcs/wcsrtombs translate between multibyte and wide at API edges — convert there, work in one form inside.
Prefer UTF-8 bytes
Store UTF-8 in char, validate explicitly, convert only where forced — the dominant modern practice.
#include <stdio.h>#include <string.h>#include <locale.h> int main(void) { setlocale(LC_ALL, ""); char utf8[] = {'c', 'a', 'f', (char)0xC3, (char)0xA9, 0}; printf("%zu bytes\n", strlen(utf8)); return 0;}Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in C yourself — building beats watching.