C Standard Library -
Introduction
is a header file in the C Standard Library used to support the internationalization and localization of programs. It provides a set of functions and macros to set or query a program's localization information, such as date, time, currency, number format, and so on.
Next we will introduce some macros, as well as an important structure struct lconv and two important functions.
Library macros
The macros defined in the header file locale.h are listed below; these macros will be used in the two functions that follow:
| No. | Macro & description |
|---|---|
| 1 | LC_ALL |
| Used to set or query all localization categories. | |
| 2 | LC_COLLATE |
| Used to set or query localization information for string comparison. | |
| 3 | LC_CTYPE |
| Used to set or query localization information for character handling. | |
| 4 | LC_MONETARY |
| Used to set or query localization information for currency formatting. | |
| 5 | LC_NUMERIC |
| Used to set or query localization information for number formatting (such as the decimal point symbol). | |
| 6 | LC_TIME |
| Used to set or query localization information for time formatting. | |
| 7 | locale_t |
| A type representing locale information. |
Library functions
The functions defined in the header file locale.h are listed below:
| No. | Function & Description |
|---|---|
| 1 | char *setlocale(int category, const char *locale) |
| Sets or reads localization information. | |
| 2 | struct lconv *localeconv(void) |
| Sets or reads localization information. | |
| 3 | locale_t newlocale(int category_mask, const char *locale, locale_t base) |
| Creates a new locale object. | |
| 4 | freelocale(locale_t locale) |
| Frees a locale object. | |
| 5 | locale_t uselocale(locale_t newloc) |
| Sets or queries a thread's locale object. |
Example
Setting and querying localization information:
Example
#include
#include
int
main
(
)
{
// Set the localization information to the default settings from the user environment variables
setlocale
(
LC_ALL
,
""
)
;
// Get and print the current locale information
printf
(
"Current locale for LC_ALL: %s\n"
,
setlocale
(
LC_ALL
,
NULL
)
)
;
printf
(
"Current locale for LC_TIME: %s\n"
,
setlocale
(
LC_TIME
,
NULL
)
)
;
printf
(
"Current locale for LC_NUMERIC: %s\n"
,
setlocale
(
LC_NUMERIC
,
NULL
)
)
;
return
0
;
}
The compilation output result is:
Current locale for LC_ALL: zh_CN.UTF-8
Current locale for LC_TIME: zh_CN.UTF-8
Current locale for LC_NUMERIC: zh_CN.UTF-8
Getting number and currency format information:
Example
#include
#include
int
main
(
)
{
// Set the localization information to the default settings from the user environment variables
setlocale
(
LC_ALL
,
""
)
;
// Get localized number and currency format information
struct
lconv
lc
=
localeconv
(
)
;
// Print number and currency format information
printf
(
"Decimal point character: %s\n"
,
lc
->
decimal_point
)
;
printf
(
"Thousands separator: %s\n"
,
lc
->
thousands_sep
)
;
printf
(
"Currency symbol: %s\n"
,
lc
->
currency_symbol
)
;
return
0
;
}
The compilation output result is:
Decimal point character: .
Thousands separator: ,
Currency symbol: ¥
Using a custom locale object:
Example
#include
#include
#include
int
main
(
)
{
// Create a new locale object using the "en_US.UTF-8" locale setting
locale_t newloc
=
newlocale
(
LC_ALL_MASK
,
"en_US.UTF-8"
,
(
locale_t
)
0
)
;
// Set the current thread's locale object to the new one
locale_t oldloc
=
uselocale
(
newloc
)
;
// Get and print the current thread's localization information
printf
(
"Current locale for LC_NUMERIC: %s\n"
,
setlocale
(
LC_NUMERIC
,
NULL
)
)
;
// Free the new locale object
uselocale
(
oldloc
)
;
freelocale
(
newloc
)
;
return
0
;
}
The compilation output result is:
Current locale for LC_NUMERIC: C
Library structure
typedef struct {
char *decimal_point;
char *thousands_sep;
char *grouping;
char *int_curr_symbol;
char *currency_symbol;
char *mon_decimal_point;
char *mon_thousands_sep;
char *mon_grouping;
char *positive_sign;
char *negative_sign;
char int_frac_digits;
char frac_digits;
char p_cs_precedes;
char p_sep_by_space;
char n_cs_precedes;
char n_sep_by_space;
char p_sign_posn;
char n_sign_posn;
} lconv
The following is a description of each field:
| No. | Field & Description |
|---|---|
| 1 | decimal_point |
| The decimal point character used for non-monetary values. | |
| 2 | thousands_sep |
| The thousands separator used for non-monetary values. | |
| 3 | grouping |
| A string representing the size of each group of digits in a non-monetary quantity. Each character represents an integer value, and each integer specifies the number of digits in the current group. A value of 0 means the previous value is applied to the remaining groups. | |
| 4 | int_curr_symbol |
| A string used for the international currency symbol. The first three characters are specified by ISO 4217:1987, and the fourth character is used to separate the currency symbol from the currency amount. | |
| 5 | currency_symbol |
| The local symbol used for currency. | |
| 6 | mon_decimal_point |
| The decimal point character used for monetary values. | |
| 7 | mon_thousands_sep |
| The thousands separator used for monetary values. | |
| 8 | mon_grouping |
| A string representing the size of each group of digits in a monetary value. Each character represents an integer value, and each integer specifies the number of digits in the current group. A value of 0 means the previous value is applied to the remaining groups. | |
| 9 | positive_sign |
| The character used for positive monetary values. | |
| 10 | negative_sign |
| The character used for negative monetary values. | |
| 11 | int_frac_digits |
| The number of digits to display after the decimal point in international monetary values. | |
| 12 | frac_digits |
| The number of digits to display after the decimal point in monetary values. | |
| 13 | p_cs_precedes |
| If equal to 1, currency_symbol appears before positive monetary values. If equal to 0, currency_symbol appears after positive monetary values. | |
| 14 | p_sep_by_space |
| If equal to 1, a space is used to separate currency_symbol from positive monetary values. If equal to 0, no space is used to separate currency_symbol from positive monetary values. | |
| 15 | n_cs_precedes |
| If equal to 1, currency_symbol appears before negative monetary values. If equal to 0, currency_symbol appears after negative monetary values. | |
| 16 | n_sep_by_space |
| If equal to 1, a space is used to separate currency_symbol from negative monetary values. If equal to 0, no space is used to separate currency_symbol from negative monetary values. | |
| 17 | p_sign_posn |
| Indicates the position of the plus sign in positive monetary values. | |
| 18 | n_sign_posn |
| Indicates the position of the minus sign in negative monetary values. |
The following values are used for p_sign_posn and n_sign_posn:
| Value | Description |
|---|---|
| 0 | parentheses enclosing the value and currency_symbol. |
| 1 | A symbol placed before the value and currency_symbol. |
| 2 | A symbol placed after the value and currency_symbol. |
| 3 | A symbol placed immediately before the value and currency_symbol. |
| 4 | A symbol placed immediately after the value and currency_symbol. |