C Programming/locale.h
In computing, locale.h is a C programming language header file, used for purposes of localization. The header provides two key functions: localeconv
and setlocale
. The former provides access to the current locale, while the latter allows one to set the current locale. The header also defines the struct lconv
, which stores information about a given locale, including the local preference for the display of numbers and currency.
Synopsis
edit#include <locale.h>
char *setlocale(int category, const char *locale);
struct lconv *localeconv(void);
Description
editsetlocale()
function
edit
The setlocale()
function is used to set or query the program's current locale.
If locale
is not NULL
, the program's current locale is modified
according to the arguments. The argument category
determines which
parts of the program's current locale should be modified.
If locale
is NULL
, the current locale is only queried, not modified.
localeconv()
function
edit
The localeconv()
function returns a pointer to a struct lconv
for the
current locale. This structure is shown in next section below, and contains all
values associated with the locale categories LC_NUMERIC and
LC_MONETARY. Programs may also use the functions printf()
and
strfmon()
, which behave according to the actual locale in use.
lconv
struct
edit
The struct lconv
contains the following fields:
Numeric (nonmonetary) information
editchar *decimal_point;
Radix character.
char *thousands_sep;
Separator for digit groups to left of radix character.
char *grouping;
Each element is the number of digits in a
group; elements with higher indices are
further left. An element with value CHAR_MAX
means that no further grouping is done. An
element with value 0
means that the previous
element is used for all groups further left.
Monetary information
editchar *int_curr_symbol;
First three chars are a currency symbol
from ISO 4217. Fourth char is the
separator. Fifth char is '\0'
.
char *currency_symbol;
Local currency symbol.
char *mon_decimal_point;
Like decimal_point
above.
char *mon_thousands_sep;
Like thousands_sep
above.
char *mon_grouping;
Like grouping
above.
char *positive_sign;
Sign for positive values.
char *negative_sign;
Sign for negative values.
char int_frac_digits;
International fractional digits.
char frac_digits;
Local fractional digits.
char p_cs_precedes;
1
if currency_symbol
precedes a
positive value, 0
if succeeds.
char p_sep_by_space;
1
if a space separates currency_symbol
from a positive value.
char n_cs_precedes;
1
if currency_symbol
precedes a
negative value, 0
if succeeds.
char n_sep_by_space;
1
if a space separates currency_symbol
from a negative value.
char p_sign_posn;
char n_sign_posn;
Positive and negative sign positions:
0
: Parentheses surround the quantity andcurrency_symbol
.1
: The sign string precedes the quantity andcurrency_symbol
.2
: The sign string succeeds the quantity andcurrency_symbol
.3
: The sign string immediately precedes thecurrency_symbol
.4
: The sign string immediately succeeds thecurrency_symbol
.
Example
edit#include <stdio.h>
#include <stdlib.h>
#include <locale.h>
int main(void)
{
/* Locale is set to "C" before this. This call sets it
to the "current locale" by reading environment variables: */
setlocale(LC_ALL, "");
const struct lconv * const currentlocale = localeconv();
printf("In the current locale, the default currency symbol is: %s\n",
currentlocale->currency_symbol);
return EXIT_SUCCESS;
}
References
edit- locale.h by OpenGroup
- localeconv by OpenGroup
- setlocale by OpenGroup