YouTip LogoYouTip

C Function Newlocale

[![Image 1: C Standard Library - ](#) C Standard Library - ](#) ## Description `newlocale()` is a function in the C standard library used to create a new locale object. This function allows programs to create a specific locale and can modify or extend it based on an existing locale. ## Declaration The declaration of the newlocale() function is as follows: #include locale_t newlocale(int category_mask, const char *locale, locale_t base); ### Parameters * `category_mask`: A mask of one or more localization categories to be set. It can be a combination of the following macros (combined using the bitwise OR operator `|`): * `LC_COLLATE_MASK`: Localization information related to string comparison. * `LC_CTYPE_MASK`: Localization information related to character classification and conversion. * `LC_MONETARY_MASK`: Localization information related to monetary formatting. * `LC_NUMERIC_MASK`: Localization information related to numeric formatting. * `LC_TIME_MASK`: Localization information related to time formatting. * `LC_MESSAGES_MASK`: Localization information related to message display. * `LC_ALL_MASK`: All localization categories. * `locale`: A string specifying the name of the locale to be set (e.g., "en_US.UTF-8"). If it is `NULL`, the default locale will be used. * `base`: An existing `locale_t` object to be used as the basis for creating a new locale object. If it is `NULL`, a new object is created from scratch. ### Return Value * On success, returns the newly created locale object (of type `locale_t`). * On failure, returns `NULL`. ## Example The following is sample code demonstrating how to use newlocale() to create and use a new locale object: ## Example #include #include #include // Required when using GNU extensions int main(){ // Create a new locale object using the "en_US.UTF-8" locale locale_t newloc = newlocale(LC_ALL_MASK,"en_US.UTF-8",(locale_t)0); if(newloc ==(locale_t)0){ perror("newlocale"); return 1; } // Set the current thread's locale object to the new locale object locale_t oldloc = uselocale(newloc); // Get and print current locale information struct lconv *lc =localeconv(); printf("Decimal point character in new locale: %sn", lc->decimal_point); printf("Thousands separator in new locale: %sn", lc->thousands_sep); // Restore previous locale object uselocale(oldloc); // Free the new locale object freelocale(newloc); return 0; } Let's compile and run the above program, which will produce the following result: Decimal point character in new locale: .Thousands separator in new locale: , ### Code Explanation Creating a new locale object * `newlocale(LC_ALL_MASK, "en_US.UTF-8", NULL)`: Creates a new locale object using the "en_US.UTF-8" locale, based on the default locale. * `newlocale(LC_NUMERIC_MASK | LC_TIME_MASK, "fr_FR.UTF-8", NULL)`: Creates a new locale object with numeric and time formats set to "fr_FR.UTF-8" (French - France), while other categories use default settings. Setting thread locale object * Use `uselocale(newloc)` to set the current thread's locale object to the newly created locale object. * Use `uselocale(oldloc)` to restore the previous locale object. Releasing locale object * Use `freelocale(newloc)` to free the newly created locale object to avoid memory leaks. ### Notes * `newlocale()` is part of the POSIX.1-2008 standard and may not be supported on all platforms. When using the GNU C Library, make sure to include the `` header file. * The `locale_t` object returned by `newlocale()` can be passed to the `uselocale()` function to temporarily use different locales within threads. ### Summary The `newlocale()` function allows the creation and use of new locale objects so that programs can handle different locales and localization requirements. By combining `newlocale()`, `uselocale()`, and `freelocale()` functions, programmers can manage and switch locale information more flexibly, supporting the development of internationalized applications. [![Image 2: C Standard Library - ](#) C Standard Library - ](#)
← C Function UselocaleScala Literals β†’