C library functions -setlocale()
C Standard Library - <locale.h>
Description
setlocaleis the C standard library<locale.h>A function used to set or query the program's localization information. It allows programmers to specify locale settings for character classification, character conversion, currency formatting, date and time formatting, numeric formatting, etc.
Declaration
Below is the declaration of the setlocale() function.
#include <locale.h> char *setlocale(int category, const char *locale);
Parameter
-
category: Specifies the localization category to set or query. It can be one of the following macros:LC_ALL: Set or query all localization categories.LC_COLLATE: Sets or queries localization information for string comparison.LC_CTYPE: Sets or queries localization information for character handling.LC_MONETARY: Sets or queries localization information for currency formatting.LC_NUMERIC: Sets or queries localization information for numeric formatting.LC_TIME: Sets or queries localization information for time formatting.
-
locale: Specifies the localization information to set. It can be one of the following:"": Sets to the default setting in the user's environment variables.NULL: Query current localization information.Specific locale names: Such as "en_US.UTF-8", used to set a specific locale.
Return Value
- if
localeisNULL, returns a pointer to the current locale information string. - if
localenotNULLand if set successfully, returns a pointer to that locale information string. - if
localenotNULLand if setting fails, returnNULL。
Example
The following is example code using setlocale, demonstrating how to set and query localization information.
Set and query localization information:
Example
#include <locale.h>
int main() {
// Query current locale information
printf("Default locale: %s\n", setlocale(LC_ALL, NULL));
// Set locale information to the default settings from the user environment variables
setlocale(LC_ALL, "");
// Get and print current locale information
printf("Locale after setting to '': %s\n", setlocale(LC_ALL, NULL));
// Set 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 us compile and run the above program, this 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
Format numbers according to localization information:
Example
#include <locale.h>
int main() {
// Set locale information to the default settings from the user environment variables
setlocale(LC_NUMERIC, "");
// Print localized floating-point number
printf("Formatted number: %'.2f\n", 1234567.89);
// Set 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 us compile and run the above program, this 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 Description
1. Set and query localization information
- At the beginning of the program, you can use
setlocale(LC_ALL, "");Set the program's localization information to the default settings in the user environment variables. User environment variables are usually set by the operating system or the user, specifying the default locale that the program should use at runtime. - You can use
setlocale(LC_ALL, "en_US.UTF-8");or other locale strings to set a specific locale. - Usage
setlocale(LC_ALL, NULL);Queries the current localization information and returns a string describing the current locale.
2. Localization categories
Each category controls specific aspects 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 numeric formatting (such as decimal point and thousands separator).LC_TIME: affects date and time formats (such asstrftime)。
Summary
setlocalefunction is an important tool for setting and querying program localization information. It allows programs to adapt to different languages and cultural conventions, thereby achieving internationalization and localization. By usingsetlocale, programs can correctly process and display localized information such as strings, numbers, currency, dates, and times.