<locale.h> controls culture-specific formatting: decimal separators, currency symbols, sorting order. By default programs run in the "C" locale (ASCII, . decimal); call setlocale(LC_ALL, "") to use the user's environment locale.
Be careful with LC_NUMERIC—changing the decimal separator can break parsing of machine-generated data.
This example demonstrates how locale affects number formatting.
// compile: gcc -o localeexample localeexample.c // run: ./localeexample // description: show how locale affects formatting #include <locale.h> #include <stdio.h> int main() { double pi = 3.141592; setlocale(LC_ALL, "C"); printf("C locale: %f\n", pi); setlocale(LC_NUMERIC, "de_DE.UTF-8"); printf("German locale: %f\n", pi); return 0; }
Locale selection:
setlocale(category, locale): set or query localelocaleconv(): get locale numeric/monetary info (returns struct lconv*)Categories (first arg to setlocale):
LC_ALL: all categoriesLC_COLLATE: string collation (strcoll, strxfrm)LC_CTYPE: character classification (isalpha, tolower)LC_MONETARY: currency formattingLC_NUMERIC: number/decimal separator (affects printf/scanf)LC_TIME: date/time formattingCommon locales:
"C": minimal C locale (ASCII, . decimal)"en_US.UTF-8": US English with UTF-8"de_DE.UTF-8": German with UTF-8"": user's environment locale