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 isLC_GLOBAL_LOCALE, then use the global locale.
    • If it isNULL, then the current locale object is not changed, and only the current locale object is returned.
    • Other validlocale_tThe object will be set as the current thread's locale object.

Return Value

  • On success, return the previouslocale_tThe object (the current thread's locale object).
  • On failure, returnsLC_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 <stdio.h>
#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

  1. Create a new locale object:

    • Usagenewlocale(LC_ALL_MASK, "en_US.UTF-8", (locale_t)0)Create a new locale object, setting the locale to "en_US.UTF-8".
  2. Print default locale information:

    • Usagelocaleconv()Get and print the current default locale information, such as the decimal point character and the thousands separator.
  3. Set the thread locale object:

    • Usageuselocale(newloc)Set the current thread's locale object to the newly created locale object, and save the previous locale object.
  4. Print new locale information:

    • Usagelocaleconv()Get and print the new locale information to verify whether the switch was successful.
  5. Restore the previous locale object:

    • Usageuselocale(oldloc)Restore the previous locale object.
  6. Release the locale object:

    • Usagefreelocale(newloc)Release the newly created locale object to prevent memory leaks.

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 environmentuselocale()You can set an independent locale object for each thread, but be aware of data consistency and race conditions between threads.
  • Usagefreelocale()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.

C Standard Library - <locale.h>

other extensions