c localization functions

This is an old revision of this page, as edited by 69.225.0.188 (talk) at 22:31, 18 June 2010 (Example: Rewritten to be more appropriate to the subject in C from C++, make use of the unnecessary stdlib.h include, and setlocale() so the currency symbol is shown portably.). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

In computing, locale.h is an C programming language header file, used for purposes of localization. The header provides two key function: localeconv and setlocale. The former provides access to the current locale, while the latter allows one to set the current locale. The header also defines the struct lconv, which stores information about a given locale, including the local preference for the display of numbers and currency.

Usage

Inclusion

C

#include <locale.h>

C++

#include <clocale>

Functions

struct lconv* localeconv(void);
char* setlocale(int, const char*);

Example

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

int
main(void)
{
    /* From setlocale(3):
    ** On startup of the main program, the portable "C""" locale
    ** is selected as default. A program may be made portable to
    ** all locales by calling setlocale(LC_ALL, """""") ... */
    setlocale(LC_ALL, """""");

    const struct lconv * const currentlocale = localeconv();

    printf
    (
        "In the current locale, the default currency symbol is: %s\n",
        currentlocale->currency_symbol
    );

    return EXIT_SUCCESS;
}

References

  1. locale.h by OpenGroup
  2. localeconv by OpenGroup
  3. setlocale by OpenGroup