locale.h in C
locale.h in C
Header files named locale.h in Library C define their own location settings, such as date format and currency symbols. You will see some macros defined along with an important structure struct lconv and two important functions listed below.
Macros are defined in locale.h
The table below lists some macros defined in locale.h and these macros will be used in the two functions shown below:
Macro Description LC_ALLSet up everything
LC_COLLATEImpact on strcoll and strxfrm functions
LC_CTYPEImpact on all character functions
LC_MONETARYImpact on currency information provided by localeconv function
LC_NUMERICImpact on decimal point format and information provided by localeconv function
LC_TIMEImpact on strftime function
Functions defined in locale.h
Here are some functions defined in locale.h of Library C:
Description function Function char * setlocale (category int, const char * locale)Setting or reading location depends on the information given
Function struct lconv * localeconv (void)Setting or reading location depends on the information given
Function setlocale () in C
Char function * setlocale (category int, const char * locale) in Library C set and read the position depending on the given information.
Declare setlocale () function in C
Here is the declaration for the setlocale () function in C:
char * setlocale ( int category , const char * locale )
Parameters
category - This is a constant (named) that determines the type of function affected by this locale setting.
LC_ALL for all functions.
LC_COLLATE for string comparison functions, such as strcoll ().
LC_CTYPE for character sorting and conversion functions, such as strtoupper ().
LC_MONETARY for currency format like localeconv ().
LC_NUMERIC for decimal separation for localeconv ().
LC_TIME for date and time format functions like strftime ().
LC _ MESSAGES for system responses.
locale - If the locale is NULL or the "empty string", the locale names will be set from the values of the environment variables with the same name as the above types.
Returns the value
If successful, setlocale () returns a string corresponding to the locale setting. This function returns NULL if the request is not executed.
For example
The following program C illustrates the usage of the setlocale () function in C:
#include #include #include int main () { time_t currtime ; struct tm * timer ; char buffer [ 80 ]; time ( & currtime ); timer = localtime ( & currtime ); printf ( "Locale la: %sn" , setlocale ( LC_ALL , "en_GB" )); strftime ( buffer , 80 , "%c" , timer ); printf ( "Date la: %sn" , buffer ); printf ( "Locale la: %sn" , setlocale ( LC_ALL , "de_DE" )); strftime ( buffer , 80 , "%c" , timer ); printf ( "Date la: %sn" , buffer ); return ( 0 ); }
Compiling and running the above C program will result:
Locale la : en_GB Date la : Fri 05 ju 2016 10 : 35 : 02 UTC Locale la : de_DE Date la : Fr 05 Dez 2016 10 : 35 : 02 UTC
Function localeconv () in C
The function struct lconv * localeconv (void) in Library C establishes and reads the position depending on the given information. They are returned in an object of the lconv structure type.
Declare the function localeconv () in C
Here is the declaration for the localeconv () function in C:
struct lconv * localeconv ( void )
Parameters
This function does not receive any parameters.
Returns the value
This function returns a pointer to a structure struct lconv for the current Locale, which has the following structure:
typedef struct { char * decimal_point ; char * thousands_sep ; char * grouping ; char * int_curr_symbol ; char * currency_symbol ; char * mon_decimal_point ; char * mon_thousands_sep ; char * mon_grouping ; char * positive_sign ; char * negative_sign ; char int_frac_digits ; char frac_digits ; char p_cs_precedes ; char p_sep_by_space ; char n_cs_precedes ; char n_sep_by_space ; char p_sign_posn ; char n_sign_posn ; } lconv
For example
The following C program illustrates the usage of the localeconv () function in C:
#include #include int main () { struct lconv * lc ; setlocale ( LC_MONETARY , "it_IT" ); lc = localeconv (); printf ( "Ky hieu cua Local Currency: %sn" , lc -> currency_symbol ); printf ( "Ky hieu cua International Currency: %sn" , lc -> int_curr_symbol ); setlocale ( LC_MONETARY , "en_US" ); lc = localeconv (); printf ( "Ky hieu cua Local Currency: %sn" , lc -> currency_symbol ); printf ( "Ky hieu cua International Currency: %sn" , lc -> int_curr_symbol ); setlocale ( LC_MONETARY , "en_GB" ); lc = localeconv (); printf ( "Ky hieu cua Local Currency: %sn" , lc -> currency_symbol ); printf ( "Ky hieu cua International Currency: %sn" , lc -> int_curr_symbol ); printf ( "Ky tu Decimal Point: %sn" , lc -> decimal_point ); return 0 ; }
Compiling and running the above C program will result:
Ky hieu cua Local Currency : EUR Ky hieu cua International Currency : EUR Ky hieu cua Local Currency : $ Ky hieu cua International Currency : USD Ky hieu cua Local Currency : £ Ky hieu cua International Currency : GBP Ky tu Decimal Point = .
Lconv structure
typedef struct { char * decimal_point ; char * thousands_sep ; char * grouping ; char * int_curr_symbol ; char * currency_symbol ; char * mon_decimal_point ; char * mon_thousands_sep ; char * mon_grouping ; char * positive_sign ; char * negative_sign ; char int_frac_digits ; char frac_digits ; char p_cs_precedes ; char p_sep_by_space ; char n_cs_precedes ; char n_sep_by_space ; char p_sign_posn ; char n_sign_posn ; } lconv
The following is a detailed description of the above fields:
The decimal_point Description fieldThe decimal point character is used for non-monetary values
thousands_sepThousands position separator characters are used for non-monetary values
groupingA string indicates the size of each digit group in non-monetary quantifiers. Each character represents an integer value, which specifies the number of digits in the current group. A value of 0 means that the previous value is used for the rest of the group
int_curr_symbolThis is a series of international currency symbols. Its first three characters are defined by ISO 4217: 1987 and the fourth character is the separation of the monetary symbol with the quantitative portion of the currency.
currency_symbolLocal icons are used for currency
mon_decimal_pointThe decimal point character is used for monetary values
mon_thousands_sepThousand group characters are used for monetary values
mon_groupingA string has elements that define the size of the digit group in monetary values. Each character represents an integer value that specifies the number of digits in the current group. A value of 0 means that the previous value is used for the rest of the group
positive_signCharacters used for positive monetary values
negative_signCharacters used for negative monetary values
int_frac_digitsNumber of digits after the decimal point in international monetary values
frac_digitsNumber of digits after the decimal point in monetary values
p_cs_precedesIf equals to 1, then the currency_symbol appears before a positive monetary value. If equals to 0, then the currency_symbol appears after a monetary value
p_sep_by_spaceIf equal to 1, then the currency_symbol separator with a positive monetary value by a space. If equal to 0, then the currency_symbol will not be decomposed and a positive monetary value
n_cs_precedesIf equals to 1, then the currency_symbol is preceded by a negative monetary value. If equals to 0, then the currency_symbol follows a negative monetary value
n_sep_by_spaceIf equal to 1, then the currency_symbol separator with a negative monetary value by a space. If 0, then no separation
p_sign_posnRepresents the position of positive_sign in a positive monetary value
n_sign_posnRepresents the position of negative_sign in a negative currency value
The following values are used for p_sign_posn and n_sign_posn in Library C:
Value Description0 Parentheses enclose the value and currency_symbol 1 The symbol precedes the value and currency_symbol 2 The symbol follows the value and currency_symbol 3 The symbol immediately precedes the value and currency_symbol 4 The symbol immediately follows the value and currency_symbolAccording to Tutorialspoint
Previous post: limits.h in C
Next lesson: math.h in C
You should read it
Maybe you are interested
Troubleshooting Chromebook Not Charging
How to use pictures as Excel chart columns
Bleach: Characters with the most changed appearances
Why do phones need wireless charging?
How to create names with special characters, add special characters to game names, game character names
How to prevent laptop battery from charging past 80% on Windows 11