liveBook · Manning

Locale

As explained in Modern C, Third Edition

A locale is a set of language- and culture-dependent conventions governing the interpretation and presentation of textual and numeric data.

Select the system locale

Call `setlocale(LC_ALL, "");` near the beginning of a program that prints extended characters. The empty string selects the system default; without the switch, terminal output may appear garbled even on a properly configured system. section 14.3

A locale supplies language-dependent conventions used by parts of the C library. Every C program starts in the normalized `"C"` locale, whose choices for numbers, times, and dates are basically American English. A program can inspect or change its current setting with `setlocale` from `<locale.h>`. Standard C requires support for `"C"` and `""`; other locale names 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 selects which behavior changes: `LC_NUMERIC` affects the decimal-point character in formatted I/O, `LC_TIME` affects `strftime`, and `LC_ALL` selects every listed category. Collation, character handling, and monetary formatting have separate categories. section 8.7

Extended text also depends on the locale. A multibyte character uses a sequence of bytes for one character; a multibyte string remains null terminated and contains no null byte inside a character. Select the native locale before working with such text, rather than assuming the initial `"C"` locale suits the terminal. section 14.3

How Modern C, Third Edition covers it

How deep it goes
Explains startup state, locale selection, category effects, and the need to switch before extended-character output.
Examples
Uses `setlocale(LC_ALL, "")` before printing phonetic symbols and other extended characters, alongside a table of category effects.
What it assumes
Assumes familiarity with C function calls and I/O while introducing locale categories and defining multibyte strings.
What you can do after
Operate/configure it with `setlocale` to select the system default or address individual categories before locale-dependent operations.
How it connects
Connects environment settings to formatted I/O, string collation, character classification, 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, Third Edition. The section links point to the parts of the book it draws on.