C library functions -uselocale()
C Standard Library - <locale.h>
Description
uselocale()It is a function in the C standard library, used to set or get the current locale object of a thread. This function allows programs to use different locale settings in different threads, thereby supporting localization in a multi-threaded environment.
Declaration
The following is the declaration of the uselocale() function.
#include <locale.h> locale_t uselocale(locale_t newloc);
Parameter
newloc: to be set as the current thread's locale object.locale_tObject.- If it is
LC_GLOBAL_LOCALE, then use the global locale. - If it is
NULL, then the current locale object is not changed, and only the current locale object is returned. - Other valid
locale_tThe object will be set as the current thread's locale object.
- If it is
Return Value
- On success, return the previous
locale_tThe object (the current thread's locale object). - On failure, returns
LC_GLOBAL_LOCALEand set the appropriate error code.
Example
The following example demonstrates how to use freelocale() to release a locale object created by newlocale().
Example
#include <locale.h>
#include <xlocale.h> // Required when using GNU extensions
int main() {
// Create a new locale object, using the "en_US.UTF-8" locale setting
locale_t newloc = newlocale(LC_ALL_MASK, "en_US.UTF-8", (locale_t)0);
if (newloc == (locale_t)0) {
perror("newlocale");
return 1;
}
// Get and print current locale information
struct lconv *lc = localeconv();
printf("Default locale decimal point: %s\n", lc->decimal_point);
printf("Default locale thousands separator: %s\n", lc->thousands_sep);
// Set the current thread's locale object to the new locale object
locale_t oldloc = uselocale(newloc);
// Get and print the new localization information
lc = localeconv();
printf("New locale decimal point: %s\n", lc->decimal_point);
printf("New locale thousands separator: %s\n", lc->thousands_sep);
// Restore the previous locale object
uselocale(oldloc);
// Get and print the restored localization information
lc = localeconv();
printf("Restored locale decimal point: %s\n", lc->decimal_point);
printf("Restored locale thousands separator: %s\n", lc->thousands_sep);
// Release the new locale object
freelocale(newloc);
return 0;
}
Let us compile and run the above program, which will produce the following result:
Default locale decimal point: . Default locale thousands separator: New locale decimal point: . New locale thousands separator: , Restored locale decimal point: . Restored locale thousands separator:
Code explanation
-
Create a new locale object:
- Usage
newlocale(LC_ALL_MASK, "en_US.UTF-8", (locale_t)0)Create a new locale object, setting the locale to "en_US.UTF-8".
- Usage
-
Print default locale information:
- Usage
localeconv()Get and print the current default locale information, such as the decimal point character and the thousands separator.
- Usage
-
Set the thread locale object:
- Usage
uselocale(newloc)Set the current thread's locale object to the newly created locale object, and save the previous locale object.
- Usage
-
Print new locale information:
- Usage
localeconv()Get and print the new locale information to verify whether the switch was successful.
- Usage
-
Restore the previous locale object:
- Usage
uselocale(oldloc)Restore the previous locale object.
- Usage
-
Release the locale object:
- Usage
freelocale(newloc)Release the newly created locale object to prevent memory leaks.
- Usage
Notes
uselocale()The function is part of the POSIX.1-2008 standard; ensure that your platform supports this function.- When used in a multi-threaded environment
uselocale()You can set an independent locale object for each thread, but be aware of data consistency and race conditions between threads. - Usage
freelocale()Release locale objects that are no longer needed to avoid memory leaks.
Summary
uselocale()The function allows setting and getting the current thread's locale object, thereby enabling localization support in multi-threaded environments. By combining the use ofnewlocale()、uselocale()andfreelocale()function, programmers can flexibly manage and switch the program's locale settings, providing independent locale support for different threads.