Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

C Library Function - uselocale()

C Library Function - uselocale()

C Standard Library - <locale.h> C Standard Library -

Description

uselocale() is a function in the C standard library used to set or get the current locale object of a thread. It allows a program to use different locale settings in different threads, thereby supporting localization in a multithreaded environment.

Declaration

Below is the declaration of the uselocale() function.



#include 



locale_t uselocale(locale_t newloc);

Parameters

- If `LC_GLOBAL_LOCALE`, the global locale is used. - If `NULL`, the current locale object is not changed; only the current locale object is returned. - Other valid `locale_t` objects will be set as the current thread's locale object.

Return Value

Example

The following example demonstrates how to use freelocale() to free a locale object created by newlocale():

Example

#include

#include

#include // 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 the 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 one

    locale_t oldloc

=

uselocale

(

newloc

)

;

   

// Get and print the new locale 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 locale information

    lc

=

localeconv

(

)

;

   

printf

(

"Restored locale decimal point: %s\n"

,

lc

->

decimal_point

)

;

   

printf

(

"Restored locale thousands separator: %s\n"

,

lc

->

thousands_sep

)

;

   

// Free the new locale object

    freelocale

(

newloc

)

;

   

return

0

;

}

Let's 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 Analysis

  1. Create a new locale object:

  2. Use newlocale(LC_ALL_MASK, "en_US.UTF-8", (locale_t)0) Create a new locale object, setting the locale to "en_US.UTF-8".
  3. - Use `newlocale(LC_ALL_MASK, "en_US.UTF-8", (locale_t)0)` to create a new locale object, setting the locale to "en_US.UTF-8".
    1. Print the default locale information:

    2. Use localeconv() Get and print the current default locale information, such as the decimal point character and thousands separator.
    3. - Use `localeconv()` to get and print the current default locale information, such as the decimal point character and thousands separator.
      1. Set the thread locale object:

      2. Use uselocale(newloc) Set the current thread's locale object to the newly created locale object, and save the previous locale object.
      3. - Use `uselocale(newloc)` to set the current thread's locale object to the newly created locale object, and save the previous locale object.
        1. Print the new locale information:

        2. Use localeconv() Get and print the new locale information to verify whether the switch was successful.
        3. - Use `localeconv()` to get and print the new locale information to verify whether the switch was successful.
          1. Restore the previous locale object:

          2. Use uselocale(oldloc) Restore the previous locale object.
          3. - Use `uselocale(oldloc)` to restore the previous locale object.
            1. Free the locale object:

            2. Use freelocale(newloc) Free the newly created locale object to prevent memory leaks.
            3. - Use `freelocale(newloc)` to free the newly created locale object to prevent memory leaks.

              Notes

              Summary

              uselocale() The function allows setting and getting the current thread's locale object, thereby enabling localization support in a multithreaded environment. Through the proper use of newlocale(), uselocale() and freelocale() the function, programmers can flexibly manage and switch the program's locale settings, providing independent locale support for different threads.

              C Standard Library - <locale.h> C Standard Library -