liveBook · Manning

Locale

As explained in Modern C

A locale is a set of environment-specific rules governing language, character classification, formatting, and encoding behavior.

Switching before extended-character output

Call `setlocale(LC_ALL, "");` near the beginning of a program that prints extended characters. The empty string selects the system’s default locale; `LC_ALL` applies the change to every locale category. Without the call, extended characters sent to a terminal may appear as garbage, even when the system is otherwise set up to display them. section 14.3 section 8.6

A locale is the language setting used by parts of the C library. Although a program inherits an execution environment that may contain `LANG` or `LC_ALL`, C starts it in the normalized `"C"` locale. That locale uses broadly American English choices for numbers, times, and dates. Use `setlocale` from `locale.h` to set or inspect the current locale. section 8.6 section 8.6

TAKEAWAY: The inherited language setting does not by itself switch the C library away from its startup locale. Select the system default explicitly when native-locale behavior is needed. section 8.6 section 14.3

Table 8.14. Categories for the setlocale function (view table figure)
LC_COLLATE String comparison through strcoll and strxfrm
LC_CTYPE Character classification and handling functions; see section 8.4.
LC_MONETARY Monetary formatting information, localeconv
LC_NUMERIC Decimal-point character for formatted I/O, localeconv
LC_TIME strftime; see section 8.5
LC_ALL All of the above

The categories identify which library behavior a locale change affects: string collation, character handling, monetary information, decimal-point formatting, and time formatting. `LC_ALL` selects all of them; a category can also be changed separately. section 8.6

Locale selection also matters for multibyte text. A multibyte character uses a sequence of bytes for one extended character; multibyte strings remain null-terminated strings. Ordinary string functions may therefore handle the string as bytes, while character-aware work calls for multibyte-aware processing. section 14.3

How Modern C covers it

How deep it goes
Explains startup and system-default selection, category effects, and an output consequence; does not specify platform-dependent locales.
Examples
Uses `setlocale(LC_ALL, "")` before printing extended characters and a category table mapping locale settings to library functions.
What it assumes
Assumes familiarity with ordinary C strings and output, while defining multibyte characters and strings in context.
What you can do after
Operate/configure it by selecting the system-default locale before extended-character output or targeting a particular C-library category.
How it connects
Connects environment variables and `setlocale` to string collation, character classification, formatted I/O, time formatting, and multibyte strings.

Also explained in

Compare how 4 books explain locale →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Modern C. The section links point to the parts of the book it draws on.