liveBook · Manning

Setlocale

As explained in Modern C, Third Edition

setlocale is a C standard-library function that selects or queries the program's current locale for locale-sensitive behavior.

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

How Modern C, Third Edition covers it

How deep it goes
Defines the call and required locale choices, maps category scopes, and demonstrates placement before text processing; does not detail return-value handling.
Examples
Shows `setlocale(LC_ALL, "")` before extended-character output and `setlocale(LC_CTYPE, "")` before scanning multibyte input.
What it assumes
Assumes familiarity with C function calls and standard I/O; defines multibyte characters when extended text becomes relevant.
What you can do after
Operate/configure it to select the system-default locale for all categories or only character handling before locale-sensitive text processing.
How it connects
Connects locale categories to character classification, formatted I/O, string comparison, time formatting, and multibyte conversion.

Also explained in

Compare how 2 books explain setlocale →

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.