Previous | Table of Contents | Next |
The header <locale.h> declares two functions that provide some support for localization issues. The notion is that there is some global state that guides certain locale-specific processing such as choice of decimal-point character, collating sequences, and so on. By default, a program starts out in the C locale, but a program can switch itself over to the users preferred locale setting by calling the setlocale function. Furthermore, the locale-specific information is separated into several categories, and it is possible to select different locale settings for different categories.
3.10.9.1. setlocale
char *setlocale(int cat, const char *locale)
setlocale requests that locale-specific settings for the given locale be instituted. The parameter cat specifies the category of settings that should be affected. The categories are all known by symbolic constants, which affect the following areas of functionality:
LC_COLLATE | String comparison as performed by strcoll and strxfrm |
LC_CTYPE | The character-classification functions in <ctype.h> |
LC_MONETARY | The currency-related settings that are available through the localeconv function |
LC_NUMERIC | The decimal-point character used by printf, scanf, strtod, and so on. |
LC_TIME | The locale-specific formats of the strftime function |
LC_ALL | The union of all the above settings |
The set of available locales is, of course, locale specific and implementation defined. A locale value of C reverts to the default C language settings. A value of (the empty string) requests an implementations or installations locale-specific default. A null pointer value for locale does not change any settings at all. Any other values have meanings defined by a particular implementation or installation.
The return value is a pointer to a string representing the previous locale setting; the previous setting may therefore be restored by passing the returned string as the locale argument in a future call to setlocale. If setlocale is called with a null pointer as the locale argument, a pointer to a string representing the current setting is returned, without changing the current setting.
3.10.9.2. localeconv
struct lconv *localeconv(void)
The localeconv function returns a pointer to a structure containing locale-specific information, all of which is a function of the currently selected locale (perhaps as set by setlocale). With the exception of the nonmonetary decimal-point character, all of this information is advisory only; no standard C library functions make use of this information. The structure is defined somewhat as follows (although the members are not required to be in this order):
struct lconv { 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, *negative_sign; char int_frac_digits; char frac_digits; char p_cs_precedes, p_sep_by_space; char n_cs_precedes, n_sep_by_space; char p_sign_posn, n_sign_posn; };
decimal_point is the decimal point character used in ordinary numeric quantities (e.g., by printf). thousands_sep is a separator that could be used between groups of digits in large numbers; grouping is a string that defines the size of the groups. The string \3 (that is, consisting of the character with value 3) would indicate repeating groups of three digits; it is also possible to indicate nonrepeating groups.
The remaining members describe (in some detail) the formatting of monetary quantities. Briefly, int_curr_symbol and currency_symbol are versions (international and local) of the local currency symbol, mon_decimal_point is the decimal point, mon_thousands_sep and mon_grouping describe the grouping (analogously to thousands_sep and grouping), positive_sign and negative_sign are sign indicators, int_frac_digits and frac_digits are the number of fractional digits to display, and the remaining values are flags controlling the arrangement of the currency symbol and sign indicator.
Previous | Table of Contents | Next |