C Library Function - setlocale()
Description
setlocale is a function in the C standard library a function in the C standard library used to set or query a program's locale information. It allows programmers to specify the locale used for character classification, character conversion, currency formatting, date and time formatting, number formatting, and so on.
Declaration
The following is the declaration of the setlocale() function.
#include
char *setlocale(int category, const char *locale);
Parameters
-
category: Specifies the locale category to set or query. It can be one of the following macros: LC_ALL: Sets or queries all locale categories.LC_COLLATE: Sets or queries locale information for string comparison.LC_CTYPE: Sets or queries locale information for character handling.LC_MONETARY: Sets or queries locale information for currency formatting.LC_NUMERIC: Sets or queries locale information for number formatting.LC_TIME: Sets or queries locale information for time formatting.
-
locale: Specifies the locale information to set. It can be one of the following: "": Sets to the default from the user's environment variables.NULL: Queries the current locale information.具体的区域设置名称: Such as "en_US.UTF-8", used to set a specific locale.
Return Value
- If both the
localeisNULL, it returns a pointer to a string containing the current locale information. - If both the
localenot"NULLand the setting succeeds, it returns a pointer to a string containing that locale information. - If both the
localenot"NULLand the setting fails, it returnsNULL.
Example
The following is sample code using setlocale that shows how to set and query locale information.
Setting and querying locale information:
Example
#include
#include
int
main
(
)
{
// Query the current locale information
printf
(
"Default locale: %s\n"
,
setlocale
(
LC_ALL
,
NULL
)
)
;
// Set the localization information to the default settings from the user environment variables
setlocale
(
LC_ALL
,
""
)
;
// Get and print the current locale information
printf
(
"Locale after setting to '': %s\n"
,
setlocale
(
LC_ALL
,
NULL
)
)
;
// Set the locale information to "en_US.UTF-8"
if
(
setlocale
(
LC_ALL
,
"en_US.UTF-8"
)
!=
NULL
)
{
printf
(
"Locale after setting to 'en_US.UTF-8': %s\n"
,
setlocale
(
LC_ALL
,
NULL
)
)
;
}
else
{
printf
(
"Unable to set locale to 'en_US.UTF-8'\n"
)
;
}
return
0
;
}
Let's compile and run the above program, which will produce the following result:
Default locale: C
Locale after setting to '': zh_CN.UTF-8
Locale after setting to 'en_US.UTF-8': en_US.UTF-8
Formatting numbers according to locale information:
Example
#include
#include
int
main
(
)
{
// Set the localization information to the default settings from the user environment variables
setlocale
(
LC_NUMERIC
,
""
)
;
// Print the localized floating-point number
printf
(
"Formatted number: %'.2f\n"
,
1234567.89
)
;
// Set the locale information to "de_DE.UTF-8" (German - Germany)
if
(
setlocale
(
LC_NUMERIC
,
"de_DE.UTF-8"
)
!=
NULL
)
{
printf
(
"Locale after setting to 'de_DE.UTF-8': %s\n"
,
setlocale
(
LC_NUMERIC
,
NULL
)
)
;
printf
(
"Formatted number in German locale: %'.2f\n"
,
1234567.89
)
;
}
else
{
printf
(
"Unable to set locale to 'de_DE.UTF-8'\n"
)
;
}
return
0
;
}
Let's compile and run the above program, which will produce the following result:
Formatted number: 1,234,567.89
Locale after setting to 'de_DE.UTF-8': de_DE.UTF-8
Formatted number in German locale: 1234567,89
Detailed explanation
- Setting and querying locale information
- At the start of a program, you can use
setlocale(LC_ALL, "");to set the program's locale information to the default from the user's environment variables. User environment variables are typically set by the operating system or the user and specify the default locale the program should use at runtime. - You can use the
setlocale(LC_ALL, "en_US.UTF-8");or other locale strings to set a specific locale. - Use
setlocale(LC_ALL, NULL);queries the current locale information and returns a string describing the current locale.
- Locale categories
Each category controls a specific aspect of the program:
LC_COLLATE: Affects string comparison functions (such asstrcollandstrxfrm).LC_CTYPE: Affects character classification and conversion functions (such asisalphaandtoupper).LC_MONETARY: Affects functions related to currency formatting (such aslocaleconv).LC_NUMERIC: Affects number formatting (such as the decimal point and thousands separator).LC_TIME: Affects date and time formatting (such asstrftime).
Summary
setlocale The function is an important tool for setting and querying a program's locale information. It allows a program to adapt to different languages and cultural conventions, thereby achieving internationalization and localization. By using setlocale, a program can correctly handle and display localized information such as strings, numbers, currency, dates, and times.
C Standard Library -