C localeconv()

The localeconv() function defined in the locale.h header file. It helps to set or read location-dependent information for the current program. These are returned in an object of the lconv structure type, and this object represents numeric and monetary formatting rules of the current C locale.


struct lconv *localeconv(void) 
 

localeconv() Parameters:

The setlocale() function doesn't take any parameters. The returned object of localeconv() can't change by the program, because it is overridden by using setlocale() function.

localeconv() Return Value

It returns a pointer to the static object which has the formatting rules of the current C locale.And it have 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
 
Input Return Value
On success a pointer to a static object
On failure a null pointer

Examples of localeconv() method in Python

Example 1: Working of localeconv() function in C


#include <stdio.h>
#include <locale.h>

int main () {
   
 struct lconv * loc;

   setlocale(LC_MONETARY, "it_IT");
   loc = localeconv();
   printf("Local Currency Symbol: %s\n",loc->currency_symbol);
   printf("International Currency Symbol: %s\n",loc->int_curr_symbol);

   setlocale(LC_MONETARY, "en_US");
   loc = localeconv();
   printf("Local Currency Symbol: %s\n",loc->currency_symbol);
   printf("International Currency Symbol: %s\n",loc->int_curr_symbol);

   setlocale(LC_MONETARY, "en_GB");
   loc = localeconv();
   printf ("Local Currency Symbol: %s\n",loc->currency_symbol);
   printf ("International Currency Symbol: %s\n",loc->int_curr_symbol);

   printf("Decimal Point = %s\n", loc->decimal_point);
   
   return 0;
  
}

Output:


Local Currency Symbol: EUR
International Currency Symbol: EUR
Local Currency Symbol: $
International Currency Symbol: USD
Local Currency Symbol: £
International Currency Symbol: GBP
Decimal Point = .

Example 2: How localeconv() function works in C?


#include <stdio.h>
#include <locale.h>

int main(int argc, const char * argv[])
{
    
    struct lconv *loc;

    /* Set the locale to the environment default */
    setlocale (LC_ALL, "");

    /* Retrieve a pointer to the current locale */
    loc = localeconv();

 
    printf("Thousands Separator: %s\n", loc->thousands_sep);
    printf("Currency Symbol: %s\n", loc->currency_symbol);

    return 0;
}

Output:


Thousands Separator: ,
Currency Symbol: $