Sun Studio 12: C User's Guide

6.9.2 The setlocale() Function

The setlocale() function is the interface to the program’s locale. In general, any program that uses the invocation country’s conventions should place a call such as:


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

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

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 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.