C Language | Advanced Features | Wide Characters - wprintf(), wscanf(), setlocale()
Wide characters are used in C when handling character sets that cannot be represented conveniently as single-byte characters. They are useful when developing internationalized software.
Internationalization
A char occupies one byte. That is sufficient for ASCII, but not for directly representing every character used by languages such as Korean. C provides the wchar_t type for wide characters. Prefix a wide character or wide string literal with L.
wchar_t wc = L'개';
wchar_t wstr[] = L"당신의 무릎에 개";
Wide strings are different from ordinary char strings. For example, use wide-string functions instead of strlen() and other functions intended for narrow strings.
Code 1
#include <stdio.h>
#include <stddef.h>
int main() {
wchar_t wc = L'개';
printf("wchar_t size = %zu\n", sizeof wc);
return 0;
}
This displays the size of a wchar_t value. Its exact size depends on the implementation.
Use wide-character I/O functions such as wprintf() and wscanf() when working with wide strings.
wprintf() Function
int wprintf(const wchar_t *format, ...);
wscanf() Function
int wscanf(const wchar_t *format, ...);
To handle locale-dependent text correctly, configure the locale with setlocale() from locale.h.
setlocale() Function
char *setlocale(int category, const char *locale);
category selects the part of the locale to change. locale identifies the desired locale. On success, the function returns a pointer to a locale description string. On failure, it returns NULL and does not change the setting.
Table 1 - Locale Category Constants
| Constant | Meaning |
|---|---|
LC_ALL |
All categories |
LC_COLLATE |
String collation |
LC_CTYPE |
Character handling |
LC_MONETARY |
Monetary formatting information |
LC_NUMERIC |
Numeric formatting information |
LC_TIME |
Date and time formatting |
Available locale names depend on the runtime environment. Passing an empty string selects the environment’s native locale. Robust programs should check the return value of setlocale().
Code 2
#include <stdio.h>
#include <stddef.h>
#include <locale.h>
#include <wchar.h>
int main() {
wchar_t wcat = L'개';
wchar_t *wstr = L"당신의 무릎에";
setlocale(LC_ALL, "");
wprintf(L"%ls%lc\n", wstr, wcat);
return 0;
}
This program selects the environment’s locale and prints a wide string and a wide character. If the runtime environment does not support the required locale, locale configuration may fail.