YouTip LogoYouTip

C Function Uselocale

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

Description

\n

uselocale() is a function in the C standard library used to set or retrieve the current locale object for a thread. This function allows programs to use different locales in different threads, thereby supporting localization in multi-threaded environments.

\n\n

Declaration

\n

The following is the declaration of the uselocale() function.

\n
#include <locale.h>locale_t uselocale(locale_t newloc);
\n\n

Parameters

\n
    \n
  • newloc: A locale_t object to be set as the current thread's locale object.\n
      \n
    • If it is LC_GLOBAL_LOCALE, the global locale is used.
    • \n
    • If it is NULL, the current locale object is not changed, and only the current locale object is returned.
    • \n
    • Other valid locale_t objects will be set as the current thread's locale object.
    • \n
    \n
  • \n
\n\n

Return Value

\n
    \n
  • On success, returns the previous locale_t object (the current thread's locale object).
  • \n
  • On failure, returns LC_GLOBAL_LOCALE and sets an appropriate error code.
  • \n
\n\n

Example

\n

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

\n\n

Example

\n
#include \n\n#include \n\n#include  // Required when using GNU extensions\n\nint main(){\n\n// Create a new locale object using "en_US.UTF-8" Locale settings\n locale_t newloc = newlocale(LC_ALL_MASK,"en_US.UTF-8",(locale_t)0);\n\nif(newloc ==(locale_t)0){\n\nperror("newlocale");\n\nreturn 1;\n\n}\n\n// Get and print the current locale information\nstruct lconv *lc =localeconv();\n\nprintf("Default locale decimal point: %sn", lc->decimal_point);\n\nprintf("Default locale thousands separator: %sn", lc->thousands_sep);\n\n// Set the current thread's locale object to the new locale object\n locale_t oldloc = uselocale(newloc);\n\n// Get and print the new locale information\n lc =localeconv();\n\nprintf("New locale decimal point: %sn", lc->decimal_point);\n\nprintf("New locale thousands separator: %sn", lc->thousands_sep);\n\n// Restore the previous locale object\n uselocale(oldloc);\n\n// Get and print the restored locale information\n lc =localeconv();\n\nprintf("Restored locale decimal point: %sn", lc->decimal_point);\n\nprintf("Restored locale thousands separator: %sn", lc->thousands_sep);\n\n// Free the new locale object\n freelocale(newloc);\n\nreturn 0;\n\n}
\n\n

Let's compile and run the above program, which will produce the following result:

\n
Default locale decimal point: .Default locale thousands separator: New locale decimal point: .New locale thousands separator: ,Restored locale decimal point: .Restored locale thousands separator:
\n\n

Code Explanation

\n
    \n
  1. Create a new locale object:\n
      \n
    • Use newlocale(LC_ALL_MASK, "en_US.UTF-8", (locale_t)0) to create a new locale object and set the locale to "en_US.UTF-8".
    • \n
    \n
  2. \n
  3. Print default locale information:\n
      \n
    • Use localeconv() to retrieve and print the current default locale information, such as the decimal point character and thousands separator.
    • \n
    \n
  4. \n
  5. Set the thread locale object:\n
      \n
    • Use uselocale(newloc) to set the current thread's locale object to the newly created locale object, and save the previous locale object.
    • \n
    \n
  6. \n
  7. Print new locale information:\n
      \n
    • Use localeconv() to retrieve and print the new locale information to verify that the switch was successful.
    • \n
    \n
  8. \n
  9. Restore the previous locale object:\n
      \n
    • Use uselocale(oldloc) to restore the previous locale object.
    • \n
    \n
  10. \n
  11. Free the locale object:\n
      \n
    • Use freelocale(newloc) to free the newly created locale object and prevent memory leaks.
    • \n
    \n
  12. \n
\n\n

Notes

\n
    \n
  • The uselocale() function is part of the POSIX.1-2008 standard; ensure your platform supports it.
  • \n
  • Using uselocale() in a multi-threaded environment allows setting independent locale objects for each thread, but be mindful of data consistency and race conditions between threads.
  • \n
  • Use freelocale() to free locale objects that are no longer needed to avoid memory leaks.
  • \n
\n\n

Summary

\n

The uselocale() function allows setting and retrieving the current thread's locale object, thereby enabling localization support in multi-threaded environments. By combining newlocale(), uselocale(), and freelocale(), programmers can flexibly manage and switch the program's locale settings, providing independent locale support for different threads.

\n\n\n C Standard Library - <locale.h> C Standard Library - <locale.h>\n
← C Function SigprocmaskC Function Newlocale β†’