Clocale Header File In C

The C programming language offers options for customization, such as date formats tailored to different cultures or currency symbols specific to certain countries. In C++, the <locale> header offers tools for managing internationalization and localization.

The essential position is indicated by the symbol "C" . The <clocale> or <locale.h> header in C++ offers functionalities and tools for managing locale-specific details. In every C programs, this is the default position that is automatically employed.

This section encompasses various statements, the functions setlocale and localeconv, and multiple macros designed for utilization with the struct lconv. These tools are utilized to define locale-specific information.

  1. Locale Settings

"Locale configurations" pertain to a collection of cultural and regional norms that impact the presentation and understanding of data like dates, times, numbers, currency, and character categorization. The present locale can be established or accessed through functions within the <clocale> header file.

  1. The responsibilities of managing locales involve:

The setlocale function has the capability to establish or fetch the current locale of the program. It requires two arguments: the category (e.g., LCNUMERIC for numeric formatting) and the specific locale name (e.g., "enUS"). This function is commonly used for configuring settings related to numeric formatting.

std::num_put: This functionality within the C++ Standard Library for localization enables the conversion of numeric values into strings according to the present locale settings.

std::numget: An aspect sensitive to locale for extracting numeric values from strings is referred to as std::numget.

  1. String Sorting

std::collate: This feature enables comparison of strings based on the conventions of the present locale through offering string collation tools.

  1. Character Categorization

std::ctype: A method that provides functionalities for categorizing characters, such as identifying if a character is a letter or a digit and converting its letter case from upper to lower.

  1. Formatting of Dates and Time

The present locale influences functions like std::strftime from the <ctime> header file, which formats dates and times based on the locale settings.

The clocale header's methods:

  • setlocale
  • localeconv
  • 1. setlocale

The setlocale function is employed to establish or inquire about the current locale of the program. It impacts the functionality of multiple functions that rely on the current locale, particularly those associated with character categorization and string transformation. In cases where you provide NULL as the locale argument, you can additionally utilize the function to retrieve the name of the presently active locale.

Language-specific configurations and location-specific details play a crucial role in interpreting and executing particular input/output and conversion tasks. The unique locale data is usually configured during runtime according to user choices or regional settings. Nevertheless, irrespective of the system's locale configuration, every C program initializes with the "C" locale established. This "C" locale is highly neutral, providing minimal locale-specific data, ensuring consistent and foreseeable program results. To leverage the default locale of the environment, this function can be invoked with an empty string as the locale argument.

The initial locale for the program is set to the "C" locale upon launch. This is equivalent to setting the locale to "C" using the setlocale(LCALL, "C") function. Invoking setlocale(LCALL, "") will adopt the locale configurations specified in the environment. The category argument specifies the specific aspects of the current locale that are impacted by this function.

Syntax:

It has the following syntax:

Example

char* setlocale (int category, const char* locale);

Parameters:

Category:

A section of the impacted area. It's one of the constant values listed below that <clocale> defines as a macro:

  • Value - The affected locale's Portion.
  • LC_ALL - The entire locale.
  • LC_COLLATE - It impacts how strcoll and strxfrm behave.
  • LC_CTYPE - It affects the multibyte and broad character functions and the character handling functions (all <cctype> functions, except isdigit and isxdigit).
  • LC_MONETARY - It modifies the monetary formatting data that localeconv
  • LC_NUMERIC - It impacts non-monetary data supplied by localeconv and the decimal-point character in prepared input/output operations and string formatting methods.
  • LC_TIME - It affects the behaviour of strftime.

Locale:

The title of a C locale is embedded within a C string. While specifics differ based on the platform, a minimum of the subsequent two locales must be available:

locale name Description
"C" Minimal "C" locale
" " Environment's default locale

The function does not alter the current locale if the value of this parameter is NULL, but it still returns the current locale's name.

Return Value:

It provides a pointer to the string that identifies the C locale following the modifications. In case no changes are made, a NULL pointer is returned. When the category is LC_ALL and different locale components have different values assigned to them, the structure of the resulting string might vary across different library implementations.

It remains unaltered, and a null reference is returned if the function is unable to establish a new locale.

Example:

Let's consider a scenario to demonstrate the usage of setlocale function with the locale.h header file in the C programming language.

Example

#include <stdio.h> 

#include <time.h> 

#include <locale.h> 

int main ()

{

time_t raw_time;

 struct tm * time_info;

 char buffer_1 [80];

 struct lconv * lc;

 time ( &raw_time );

 time_info = localtime ( &raw_time );

 int twice_1=0;

 do {

 printf ("The locale is: %s\n", setlocale(LC_ALL,NULL) );

 strftime (buffer_1,80,"%c",time_info);

 printf ("Date is: %s\n",buffer_1);

 lc = localeconv ();

 printf ("The currency symbol is: %s\n-\n",lc->currency_symbol);

 setlocale (LC_ALL,"");

 } while (!twice_1++);

 return 0;

}

Output:

Output

The locale is: C

Date is: Wed Nov 29 17:07:13 2023

The currency symbol is: 

-

The locale is: C

Date is: Wed Nov 29 17:07:13 2023

The currency symbol is: $

-

Explanation:

  • In this program, we include several header files, including <time.h> , which is used to work with time-related functions, <locale.h> , which is used for localization and internationalization routines; and <stdio.h> is used for input and output operations.
  • The program declares variables required for time and location operations. Buffer1 is an array that stores the formatted date and time; timet is a data type that represents time; struct tm is a structure that holds time components; and struct lconv is a structure that stores information about formatting numeric and monetary values.
  • The current time is obtained using the time function, and the localtime function transforms the raw time value into a struct tm structure that represents the local time.
  • A do-while loop is inserted into the program. Every time, it fetches the current locale and prints it using setlocale with LC_ALL as the category and NULL as the second parameter.
  • The format supplied ( "%c" for the chosen date and time representation) is applied to the date and time using strftime. After being prepared and saved in buffer_1 , the output is printed.
  • The currency symbol is printed, and localeconv is used to obtain details about the monetary formatting of the current locale.
  • Until twice1 gets non-zero, the loop is continued. It uses setlocale(LCALL, "") to return the locale to its default state after each loop.
  • The program ends by returning 0, which denotes a successful run. In the first loop iteration, the output will contain the locale, date and time, and currency symbol for the default locale. The output in the second loop iteration will have the exact data for the user's default locale.
  • 2. Localeconv

The value returned by this function encapsulates the currency and numerical formatting guidelines specific to the current C locale. The associated header file is. The "c" locale serves as the basic locale. Nonetheless, the outcome remains consistent across compilers due to the identical settings in the locale. It is the default setting for all C programs.

Syntax:

It has the following syntax:

Example

struct lconv* localeconv (void);

It retrieves the locale formatting configurations for numerical values.

Obtain the locale formatting settings for the quantities.

  • localeconv: It is the function's name. The format conventions of the current C locale are returned by this standard C library function.
  • struct lconv*: It is the function's return type. It indicates that a pointer to a structure of type lconv is returned by the function. The lconv structure is defined in the <clocale> header and contains fields with information about numeric and monetary formatting.
  • (void): This part states that there are no arguments required by the localeconv An older notation that indicates the function has no parameters is (void) . An empty parameter list can likewise be used in C++ to indicate the absence of parameters.

Example:

Let's consider an example to demonstrate the usage of the localeconv function with the <locale.h> header in C.

Example

#include <stdio.h> 

#include <locale.h> 

int main ()

{

 setlocale (LC_MONETARY,"");

 struct lconv * lc;

 lc=localeconv();

 printf ("Local Currency Symbol: %s\n",lc->currency_symbol);

 printf ("International Currency Symbol: %s\n",lc->int_curr_symbol);

 return 0;

}

Output:

Output

Local Currency Symbol: $

International Currency Symbol: USD

Explanation:

  • In this example, we include several header files, including <locale.h> , which is used for localization and internationalization procedures, and <stdio.h> is used for input and output operations.
  • In this instance, the program's locale is set using the setlocale function. The category for formatting monetary values is LC_MONETARY , and utilizing the user's default locale is indicated by the empty string " " as the second input. The program can adjust to the user's regional settings using this function call.
  • A structure called struct lconv contains data regarding how monetary and numeric values should be formatted. The localeconv function returns the monetary formatting information for the current locale as a pointer to a structure of type struct lconv.
  • After that, the program prints two bits of information on currency formatting. lc->currencysymbol: The local currency symbol. lc->intcurr_symbol: It is the international currency symbol.
  • The localeconv function populated the struct lconv structure, which yields these values.
  • When the program finally returns 0, it has successfully executed. Depending on the user's default locale, the program's output will include local and foreign currency symbols.
  • lc->currency_symbol: The local currency symbol.
  • lc->intcurrsymbol: It is the international currency symbol.

Input Required

This code uses input(). Please provide values below: