liveBook · Manning

Setlocale

As explained in Modern C

setlocale is a C standard library function that queries or changes the locale applicable to specified categories of locale-sensitive operations.

Select the system default locale

setlocale(LC_ALL, ""); selects the system’s default locale for all locale categories. Calling it before printing extended characters can help them display correctly, provided the system is suitably configured. section 8.6 section 14.3

setlocale, declared in <locale.h>, sets or inspects the locale used by locale-sensitive operations. Its category argument selects all or part of the language environment. section 8.6

At program startup, C selects the "C" locale, with essentially American English conventions for numbers, times, and dates. The C standard also provides the empty string "" to select the system default; other available settings depend on the platform. section 8.6

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 category determines which library operations are affected: for example, LC_NUMERIC controls the decimal-point character in formatted I/O, while LC_CTYPE covers character classification and handling. LC_ALL selects all listed categories; a platform may provide others. section 8.6

TAKEAWAY: Selecting the system default is important when working with extended characters, but it does not make a byte count a character count. A multibyte string can contain characters represented by more than one byte. section 14.3

How Modern C covers it

How deep it goes
Explains startup defaults and category effects, with a selection example, but does not detail query or return-value behavior.
Examples
Shows setlocale(LC_ALL, "") before printing a line containing extended characters and box-drawing symbols.
What you can do after
Apply it via the C standard library to select the system default locale for locale-sensitive operations.
How it connects
Connects locale categories to character classification, string comparison, numeric formatting, and multibyte strings.

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. The section links point to the parts of the book it draws on.