Select the native locale
Near the beginning of a program, call `setlocale(LC_ALL, "");` before printing text containing extended characters. The empty string requests the system-default locale; `LC_ALL` applies it to every locale category. Without the switch, the program starts in the `"C"` locale, and extended-character output may appear garbled. section 8.7 section 14.3
Declared in `<locale.h>`, `setlocale` selects or inspects the current locale. A locale governs language-sensitive behavior in parts of the C library. The C standard requires support for the `"C"` locale and for `""` as a request for the system default; additional locale values depend on the platform. section 8.7
Table 8.17 Categories for the setlocale function
| Category | Scope |
LC_COLLATE |
String comparison through strcoll and strxfrm |
LC_CTYPE |
Character classification and handling functions; see subsection 8.5. |
LC_MONETARY |
Monetary formatting information, localeconv |
LC_NUMERIC |
Decimal-point character for formatted I/O, localeconv |
LC_TIME |
strftime; see subsection 8.6 |
LC_ALL |
All of the above |
The category table maps each `setlocale` category to the library behavior it affects, from string comparison and numeric formatting to character handling and time formatting. section 8.7
Choose a category according to the behavior that must change:
- Use `LC_ALL` to select every listed category at once. section 8.7
- Use `LC_CTYPE` when character handling and multibyte text are the concern. section 8.7 section 14.5
- Use `LC_NUMERIC` for the decimal-point character in formatted I/O; `LC_TIME` affects `strftime`. section 8.7
Read multibyte input
A text-conversion program begins with `setlocale(LC_CTYPE, "");` before reading input and converting it with `mbrtoc8`. This selects the platform’s multibyte encoding for character handling. Without the call, the scan remains in the `"C"` locale and processes input one byte at a time. section 14.5
TAKEAWAY: Set the relevant locale before relying on locale-sensitive text behavior. Selecting the system default is not, by itself, a guarantee of particular terminal output: the extended-character example also depends on a properly installed system. section 14.3 section 14.5

