The ANSI standard supports the development of C programs that are adaptable to language and country-specific customs, such as the formatting of currency strings. The ANSI library also provides two functions, the type lconv, and macros for dealing with locales. These are declared in the header file locale.h.
All programs start with the default locale "C", which contains no country or language-specific information. During execution, the program can change to another locale and retrieve locale-specific information. Since most applications do not require the full range of locale-specific information, this information is classified into categories, as shown in Table 1-42.
The following function is used to adapt a program to a specific locale:
char *setlocale ( int category , const char *name );
The argument category is one of the symbolic constants described in Table 1-42, and name points to a string which identifies the desired locale for the specified category.
The name string may have at least the following values:
"C"
The default locale, with no country-specific information.
""
The compiler's native locale.
NULL
setlocale() makes no changes, but returns the name of the current locale. This name can later be passed to setlocale() as an argument to restore the locale after it has been changed.
The following standard function groups use locale information: formatted input/output, character classification and case mapping, multibyte character handling, multibyte string handling, and conversion between strings and numeric values.
The following function can be used to obtain information for formatting numeric strings, such as the decimal point and currency symbol characters:
struct lconv* localeconv ( void );
Fills in a structure of type struct lconv with the values defined by the current locale. The members of this structure type must include at least those shown in the following example. The sample values in parentheses are those for Switzerland:
struct lconv {
// Information for non-currency values:
char *decimal_point; // The decimal character
// (".")
char *thousands_sep; // The character used to group
// digits left of the decimal
// point (",")
char *grouping; // Number of digits in each group
// ("\3")
// Information for currency values:
char *int_curr_symbol; // The three-letter symbol for
// the local currency per ISO
// 4217, with a separator
// character ("CHF ")
char *currency_symbol; // The local currency
// symbol ("SFrs.")
char *mon_decimal_point; // The decimal point character
// for currency strings (".")
char *mon_thousands_sep; // The character used to group
// digits left of the decimal
// point (".")
char *mon_grouping; // Number of digits in each group
// ("\3")
char *positive_sign; // Sign for positive
// currency strings ("")
char *negative_sign; // Sign for negative
// currency strings ("C")
char int_frac_digits; // Number of digits after the
// decimal point in the
// international format (2)
char frac_digits; // Number of digits after the
// decimal point in the local
// format (2)
char p_cs_precedes; // For non-negative values:
// 1 = currency symbol is before,
// 0 = after the amount (1)
char p_sep_by_space; // For non-negative values:
// 1 = currency symbol is before,
// 0 = after the amount (1)
char n_cs_precedes; // For negative values:
// 1 = currency symbol is before,
// 0 = after the amount (1)
char n_sep_by_space; // For negative values:
// 1 = space, 0 = no space
// between currency
// symbol and amount (0)
char p_sign_posn; // Position of positive_sign (1)
char n_sign_posn; // Position of negative_sign (2)
char int_p_cs_precedes; // For non-negative
// internationally formatted
// values:
// 1=space, 0 = no space
// between currency symbol
// and amount (1)
char int_p_sep_by_space; // For non-negative
// internationally formatted
// values:
// 1 = space, 0 = no space
// between currency symbol
// and amount (0)
char int_n_cs_precedes; // For negative internationally
// formatted values:
// 1= currency symbol precedes
// amount, 0 = currency symbol
// follows amount (1)
char int_n_sep_by_space; // For negative internationally
// formatted values:
// 1 = space, 0 = no space
// between symbol and amount (0)
char int_p_sign_posn; // Position of positive sign for
// internationally formatted
// values (1)
char int_n_sign_posn; // Position of negative sign for
// internationally formatted
// values (2)
};
If the value of p_sign_posn, n_sign_posn, int_p_sign_posn, or int_n_sign_posn is 0, the amount and the currency symbol are set in parentheses. If 1, the sign string is placed before the amount and the currency symbol. If 2, the sign string is placed after the amount and the currency symbol. If 3, the sign string immediately precedes the currency symbol. If 4, the sign string is placed immediately after the currency symbol.
The value \3 in the strings grouping and mon_grouping means that each group consists of three digits, as in "1,234,567.89".