Skip to main content

locale.h

Introduction

locale.h is the localisation setting for the program and mainly affects the following behaviour.

  • Number format
  • Currency formats
  • Character sets
  • Date and time formats

It sets the following macros.

  • LC_COLLATE: affects the string comparison functions strcoll() and strxfrm().
  • LC_CTYPE: affects the behaviour of the character handling functions.
  • LC_MONETARY: affects the currency format.
  • LC_NUMERIC: affects the numeric format of printf().
  • LC_TIME: affects the time formats strftime() and wcsftime().
  • LC_ALL: sets all the above categories to the given locale.

setlocale()

`setlocale() is used to set the current locale.

``c char setlocal(int category, const char locale);


It accepts two arguments. The first argument indicates the range of influence, and if the value is one of the previous five macros indicating category, only the category corresponding to that macro will be affected, or if the value is ``LC_ALL``, all categories will be affected. The second parameter is usually only `"C"` (normal mode) or `""` (local mode).

The following call is implied at the start of any program.

```c
setlocale(LC_ALL, "C");

The following statement localises the format.

setlocale(LC_ALL, "");

In the above example, the second argument is a null character, indicating that the localisation settings provided by the current environment are used.

In theory, the second parameter could also be set to some format supported by the current system.

setlocale(LC_ALL, "en_US.UTF-8");

However, this makes the program less portable, as there is no guarantee that other systems will also support that format. Therefore, it is common to set the second parameter to an empty string and use the current settings of the operating system.

The return value of setlocale() is a pointer to a string indicating the format that has been set. If the call fails, the null pointer NULL is returned.

`setlocale() can be used to query the current locale, in which case the second argument is set to NULL and that's it.

char *loc;

loc = setlocale(LC_ALL, NULL);

// output Starting locale: C
printf("Starting locale: %s\n", loc);

loc = setlocale(LC_ALL, "");

// output Native locale: en_US.UTF-8
printf("Native locale: %s\n", loc);
ðŸ˜'

## localeconv()

``localeconv()` is used to get the details of the current format.

```c
struct lconv* localeconv(void);

This function returns a pointer to a Struct structure which contains format information, its main attributes are as follows.

  • char* mon_decimal_point: the decimal decimal point character of the currency, e.g. . .
  • char* mon_thousands_sep: the thousands separator character for the currency, e.g. ,.
  • char* mon_grouping: the grouping descriptor for the currency.
  • char* positive_sign: the positive sign of the currency, e.g. + or as an empty string.
  • char* negative_sign: negative sign of the currency, e.g. -.
  • char* currency_symbol: currency symbol, e.g. $.
  • char frac_digits: the number of decimal places to output after the decimal point when printing currency amounts, e.g. set to 2.
  • char p_cs_precedes: when set to 1, the currency symbol currency_symbol appears before the non-negative amount. When set to 0, it appears after it.
  • char n_cs_precedes: set to 1, the currency symbol currency_symbol appears before a negative currency amount. When set to 0, it appears after.
  • char p_sep_by_space: determines the character separating a non-negative currency amount from the currency symbol.
  • char n_sep_by_space: determines the character separating a negative currency amount from the currency sign.
  • char p_sign_posn: determines the position of a positive sign for a non-negative value.
  • char n_sign_posn: determines the position of the negative sign for negative values.
  • char* int_curr_symbol: The international symbol for the currency, e.g. USD.
  • char int_frac_digits: the value of frac_digits when the international symbol is used.
  • char int_p_cs_precedes: the value of p_cs_precedes when the international symbol is used.
  • char int_n_cs_precedes: the value of n_cs_precedes when international notation is used.
  • char int_p_sep_by_space: the value of p_sep_by_space when international notation is used.
  • char int_n_sep_by_space: value of n_sep_by_space when using international symbols.
  • char int_p_sign_posn: value of p_sign_posn when using international symbols.
  • char int_n_sign_posn: the value of n_sign_posn when international symbols are used.

The following program prints the value of the current system's attributes.

#include <stdio.h>
#include <locale.h
#include <string.h>

int main ()
{
setlocale (LC_ALL, "zh_CN");
struct lconv * lc;
lc=localeconv();
printf ("decimal_point: %s\n",lc->decimal_point);
printf ("various_sep: %s\n",lc->thousands_sep);
printf ("grouping: %s\n",lc->grouping);
printf ("int_curr_symbol: %s\n",lc->int_curr_symbol);
printf ("currency_symbol: %s\n",lc->currency_symbol);
printf ("mon_decimal_point: %s\n",lc->mon_decimal_point);
printf ("mon_thousands_sep: %s\n",lc->mon_thousands_sep);
printf ("mon_grouping: %s\n",lc->mon_grouping);
printf ("positive_sign: %s\n",lc->positive_sign);
printf ("negative_sign: %s\n",lc->negative_sign);
printf ("frac_digits: %d\n",lc->frac_digits);
printf ("p_cs_precedes: %d\n",lc->p_cs_precedes);
printf ("n_cs_precedes: %d\n",lc->n_cs_precedes);
printf ("p_sep_by_space: %d\n",lc->p_sep_by_space);
printf ("n_sep_by_space: %d\n",lc->n_sep_by_space);
printf ("p_sign_posn: %d\n",lc->p_sign_posn);
printf ("n_sign_posn: %d\n",lc->n_sign_posn);
printf ("int_frac_digits: %d\n",lc->int_frac_digits);
printf ("int_p_cs_precedes: %d\n",lc->int_p_cs_precedes);
printf ("int_n_cs_precedes: %d\n",lc->int_n_cs_precedes);
printf ("int_p_sep_by_space: %d\n",lc->int_p_sep_by_space);
printf ("int_n_sep_by_space: %d\n",lc->int_n_sep_by_space);
printf ("int_p_sign_posn: %d\n",lc->int_p_sign_posn);
printf ("int_n_sign_posn: %d\n",lc->int_n_sign_posn);

return 0;
}