C Standard Library -<locale.h>
Introduction
<locale.h>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 the program's localization information, such as dates, times, currency, number formats, etc.
Next, we will introduce some macros, as well as an important structure.struct lconvand 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 listed below:
| Serial Number | Macros & 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 processing. |
| 4 | LC_MONETARY Used to set or query localization information for currency format. |
| 5 | LC_NUMERIC Used to set or query localization information for number format (e.g., the decimal point symbol). |
| 6 | LC_TIME Used to set or query localization information for time format. |
| 7 | locale_t Types that represent locale information. |
Library functions
The functions defined in the header file locale.h are listed below:
| Serial Number | Functions & 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 localization object. |
| 4 | freelocale(locale_t locale) Releases a localization object. |
| 5 | locale_t uselocale(locale_t newloc) Sets or queries the thread's localization object. |
Example
Setting and querying localization information:
Example
#include <locale.h>
int main() {
// Set locale information to the default settings from the user environment variables
setlocale(LC_ALL, "");
// Get and print 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;
}
Compilation output 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 numeric and currency format information:
Example
#include <locale.h>
int main() {
// Set locale information to the default settings from the user environment variables
setlocale(LC_ALL, "");
// Get localized numeric and monetary formatting information
struct lconv *lc = localeconv();
// Print numeric and monetary formatting 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;
}
Compilation output is:
Decimal point character: . Thousands separator: , Currency symbol: ¥
Using a custom localization object:
Example
#include <locale.h>
#include <xlocale.h>
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 locale object
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));
// Release the new locale object
uselocale(oldloc);
freelocale(newloc);
return 0;
}
Compilation output 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:
| Serial Number | Fields & 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 non-monetary quantities. 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 will apply to the remaining groupings. |
| 4 | int_curr_symbol The 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 monetary values. 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 will apply to the remaining groupings. |
| 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 be displayed after the decimal point in international monetary values. |
| 12 | frac_digits The number of digits to be displayed after the decimal point in monetary values. |
| 13 | p_cs_precedes If equal to 1, currency_symbol appears before the positive monetary value. If equal to 0, currency_symbol appears after the positive monetary value. |
| 14 | p_sep_by_space If equal to 1, a space is used to separate currency_symbol and the positive monetary value. If equal to 0, no space is used between currency_symbol and the positive monetary value. |
| 15 | n_cs_precedes If equal to 1, currency_symbol appears before the negative monetary value. If equal to 0, currency_symbol appears after the negative monetary value. |
| 16 | n_sep_by_space If equal to 1, a space is used to separate currency_symbol and the negative monetary value. If equal to 0, no space is used between currency_symbol and the negative monetary value. |
| 17 | p_sign_posn Indicates the position of the positive sign in positive monetary values. |
| 18 | n_sign_posn Indicates the position of the negative sign in negative monetary values. |
The following values are used forp_sign_posnandn_sign_posn:
| Value | Description |
|---|---|
| 0 | The parentheses enclosing the value and currency_symbol. |
| 1 | The symbol placed before the value and currency_symbol. |
| 2 | The symbol placed after the value and currency_symbol. |
| 3 | The symbol placed immediately before the value and currency_symbol. |
| 4 | The symbol placed immediately after the value and currency_symbol. |