The <clocale>
header (from the C standard <locale.h>
header) declares types and functions to support internationalization and localization for the C standard library. C++ also offers <locale>
, which has more flexibility and functionality, but at a cost of complexity and overhead.
int LC_ALL
The LC_ALL
macro expands to a constant integer, to be used to set all categories in a call to setlocale
.
int LC_COLLATE
The LC_COLLATE
macro expands to a constant integer, to be used to set the collation category in a call to setlocale
. The collation order is used by functions such as strcoll
.
int LC_CTYPE
The LC_CTYPE
macro expands to a constant integer, to be used to set the character type category in a call to setlocale
. The character type information is used by the <cctype>
functions such as isalpha
.
int LC_MONETARY
The LC_MONETARY
macro expands to a constant integer, to be used to set the monetary formatting category in a call to setlocale
. Call localeconv
to retrieve this information.
int LC_NUMERIC
The LC_NUMERIC
macro expands to a constant integer, to be used to set the numeric formatting category in a call to setlocale
. I/O functions such as printf
use this information to format numbers.
int LC_TIME
The LC_TIME
macro expands to a constant integer, to be used to set the time formatting category in a call to setlocale
. The strftime
function uses this information.
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; 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; };
The lconv
structure stores information used to format numbers and monetary values. An implementation can add more members to the class and can change the order of declaration.
Each locale defines suitable values for the lconv
members. The char
-type members are all non-negative integers, where CHAR_MAX
means the information is not available in the current locale. For the char*
members, an empty string means the information is not available. The "C"
locale uses "."
for decimal_point
, an empty string (""
) for all other char*
members, and CHAR_MAX
for all char
members. All strings are null-terminated.
The localeconv
function returns a pointer to the current locale's lconv
object.
Following are descriptions of the lconv
members:
char* currency_symbol
"$"
).char* decimal_point
"."
.char frac_digits
char* grouping
'\0'
means to repeat the last grouping for the rest of the value. The value CHAR_MAX
means not to group the remaining values. Any other value is the size of a digit group. The next character in the string specifies the size of the preceding group. Thus, the grouping string is read in the opposite order of the formatted digits."\3"
, to format digits in groups of three, e.g., "1,234,567."char* int_curr_symbol
int_curr_symbol
would be "USD "
.char int_frac_digits
char* mon_decimal_point
char* mon_grouping
grouping
).char* mon_thousands_sep
char n_cs_precedes
char n_sep_by_space
char n_sign_posn
Value | Position |
---|---|
0 | Parentheses surround the value and the currency symbol |
1 | The sign precedes the value and currency symbol |
2 | The sign follows the value and currency symbol |
3 | The sign appears immediately before the currency symbol |
4 | The sign appears immediately after the currency symbol |
char* negative_sign
"-"
).char p_cs_precedes
char p_sep_by_space
char p_sign_posn
char* positive_sign
char* thousands_sep
","
).lconv* localeconv();
The localeconv
function returns a pointer to the current locale's lconv
object. You must not modify the lconv
object, and a subsequent call to localeconv
might overwrite its contents. Calls to setlocale
for LC_ALL
, LC_NUMERIC
, or LC_MONETARY
categories might also overwrite the contents of the lconv
object.
#define NULL 0
The NULL
macro is defined in several C headers. See its description in <cstddef>
for details.
char* setlocale(int category, const char* locale)
The setlocale
function sets the locale for a specific category
, which must be one of the LC_
macros.
The locale
parameter is the name of the locale. The default for all categories is the "C"
locale. The empty string (""
) is an implementation-defined native locale. The implementation defines other possible values for locale
.
To query the current locale, pass NULL
as the locale
. Note that each category might have a different locale, so the return value might contain multiple locale names.
The return value is a pointer to a string associated with the given category and new locale (or current locale if querying with a NULL
locale
parameter). If the locale cannot be set, a null pointer is returned.
Do not modify the string returned from setlocale
. A subsequent call to setlocale
might overwrite the contents of the string returned from an earlier call (or simultaneous call in a multithreaded program).