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.

A locale is a set of language- and culture-dependent conventions for interpreting and presenting data. In C, it affects operations such as character classification, string comparison, numeric formatting, and date and time formatting. section 8.7

 setlocale(LC_ALL, "");
   /* Multibyte character printing only works after the locale
      has been switched. */
   draw_sep(TOPLEFT " © 2014 j𝜀nz ' g𝜔z,t𝜀t ", TOPRIGHT);

Switch to the system’s native locale near the start of a program before printing extended characters. Without the switch, terminal output may be garbled; correct display also depends on the system being properly configured. section 14.3

At startup, C sets the locale to "C", with essentially American English conventions for numbers, times, and dates. The setlocale function in <locale.h> sets or inspects the current locale. The standard also guarantees the locale name "", which selects the system default. Environment variables such as LANG and LC_ALL commonly provide language settings, though their availability depends on the operating system. 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 argument selects which conventions setlocale addresses. LC_ALL covers all listed categories; a narrower category changes or inspects only the corresponding part of the language environment. Additional categories may be platform-dependent. section 8.7

A multibyte character is a sequence of bytes representing one extended character. Multibyte characters contain no null bytes, and multibyte strings are null terminated, so ordinary string functions such as strcpy can work with them. section 14.3

How Modern C, Third Edition covers it

How deep it goes
Explains startup defaults, setlocale and standard categories, while noting that additional categories can be platform-dependent.
Examples
Uses native-locale initialization before printing extended characters and a table of setlocale categories.
What you can do after
Operate and configure locale settings using setlocale to select the system default or address particular categories.
How it connects
Connects locale to environment variables, character classification, string comparison, formatted I/O 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.