totn C Functions

C Language: setlocale function
(Set Locale)

In the C Programming Language, the setlocale function allows you to set the program's locale information.

Syntax

The syntax for the setlocale function in the C Language is:

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

Parameters or Arguments

category
The program's locale to change. It can either be one category or all categories.
locale
A pointer to a string which contains the new locale.

Note

  • If category is LC_COLLATE, LC_CTYPE, LC_MONETARY, LC_NUMERIC, or LC_TIME macro, the setlocale function will only update one category.
  • If category is LC_ALL, the setlocale function will update all categories.

Returns

If locale is a null pointer, the setlocale function returns a pointer to the string associated with category for the current locale. If the locale is not a null pointer, the setlocale function returns a pointer to the string associated with category for the new locale.

If the setlocale function fails, a null pointer will be returned.

Required Header

In the C Language, the required header for the setlocale function is:

#include <locale.h>

Applies To

In the C Language, the setlocale function can be used in the following versions:

  • ANSI/ISO 9899-1990

setlocale Example

/* Example using setlocale by TechOnTheNet.com */

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

int main(int argc, const char * argv[])
{
    /* Define a temporary variable */
    struct lconv *loc;

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

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

    /* Display some of the locale settings */
    printf("Thousands Separator: %s\n", loc->thousands_sep);
    printf("Currency Symbol: %s\n", loc->currency_symbol);

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

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

    /* Display some of the locale settings */
    printf("Thousands Separator: %s\n", loc->thousands_sep);
    printf("Currency Symbol: %s\n", loc->currency_symbol);

    return 0;
}

When compiled and run on a machine in North America, this application will output:

Thousands Separator: 
Currency Symbol: 
Thousands Separator: ,
Currency Symbol: $

Similar Functions

Other C functions that are similar to the setlocale function: