setlocale function(Set Locale) in C Language – example and detail

In the C Programming Language, the setlocale function lets in you to set the program’s locale information.

Syntax

The syntax for the setlocale characteristic 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 includes the new locale.

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

Returns

If locale is a null pointer, the setlocale feature returns a pointer to the string related with category for the modern-day locale. If the locale is now 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 characteristic 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 computer in North America, this application will output:

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

Similar Functions

Other C features that are similar to the setlocale function:

localeconv function <locale.h>