Site Tools


h-locale

<locale.h>

<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.

Example

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;
}

Common functions and macros

Locale selection:

  • setlocale(category, locale): set or query locale
  • localeconv(): get locale numeric/monetary info (returns struct lconv*)

Categories (first arg to setlocale):

  • LC_ALL: all categories
  • LC_COLLATE: string collation (strcoll, strxfrm)
  • LC_CTYPE: character classification (isalpha, tolower)
  • LC_MONETARY: currency formatting
  • LC_NUMERIC: number/decimal separator (affects printf/scanf)
  • LC_TIME: date/time formatting

Common 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
h-locale.md · Last modified: by 127.0.0.1