Locale Settings Dear to You

The setlocale() function, mentioned in last week’s Lesson, both returns the current locale string and sets a new locale. It’s not necessary to call this function to gather locale details already known to your computer. To pluck out this detail, use the localeconv() function.

Here’s the format:

struct lconv * localeconv(void)

Like the setlocale() function, localeconv() is prototyped in the locale.h header file. The function takes no argument and returns a pointer to an lconv structure. This structure’s members contain the various tidbits related to localization items.

The full list of lconv structure members is found by view the localeconv() function’s man page. They may differ from system to system. Some of the members may not be available on all systems.

The following code displays three members of the lconv structure:

decimal_point is a char pointer (string) representing the decimal point character(s).

thousands_sep is a char pointer (string) representing the character(s) used to separate thousands in a large value.

currency_symbol is a char pointer (string) representing the character(s) used as to represent a monetary value for a locale.

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

int main()
{
    struct lconv *locale_ptr;

    locale_ptr = localeconv();

    printf("Decimal point: %s\n",locale_ptr->decimal_point);
    printf("Thousands separator: %s\n",locale_ptr->thousands_sep);
    printf("Currency symbol: %s\n",locale_ptr->currency_symbol);

    return(0);
}

Because lconv structure variable locale_ptr is a pointer, structure member pointer notation is used at Lines 10 through 12. Here’s sample output:

Decimal point: .
Thousands separator:
Currency symbol:

On my system, the period is the decimal point character. The other two items are undefined, so their strings are empty. This output seems disappointing, but my guess is that the operating system most likely has better locale functions. It’s probably what most programs use when outputting region-specific information.

For your C code, the locale functions come into play when generating wide-characters. You can read this blog’s series on wide characters here.

Leave a Reply