Oracle® Solaris Studio 12.4: C User's Guide

Exit Print View

Updated: March 2015
 
 

6.8.2 setlocale() Function

The setlocale() function is the interface to the program’s locale. Any program that uses the invocation country’s conventions should place a call such as the following example early in the program’s execution path.

#include <locale.h>
/*...*/
setlocale(LC_ALL, "");

This call causes the program’s current locale to change to the appropriate local version, because LC_ALL is the macro that specifies the entire locale instead of one category. The standard categories are:

LC_COLLATE

Sorting information

LC_CTYPE

Character classification information

LC_MONETARY

Currency printing information

LC_NUMERIC

Numeric printing information

LC_TIME

Date and time printing information

Any of these macros can be passed as the first argument to setlocale() to specify that category.

The setlocale() function returns the name of the current locale for a given category (or LC_ALL) and serves in an inquiry-only capacity when its second argument is a null pointer. Thus, code similar to the following example can be used to change the locale or a portion thereof for a limited duration:

#include <locale.h>
/*...*/
char *oloc;
/*...*/
oloc = setlocale(LC_category, NULL);
if (setlocale(LC_category, "new") != 0)
{
        /* use temporarily changed locale */
    (void)setlocale(LC_category, oloc);
}

Most programs do not need this capability.