Developer's Topics
Chapter 2, Complying with standard C

The setlocale() function

The setlocale() function

The setlocale() function is the interface to the program's locale. In general, any program that requires 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 causes the program's current locale to change to the appropriate local version (if possible), 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
 LC_MESSAGES   message language information
Any of these macros can be passed as the first argument to setlocale() to specify just 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 along the lines of 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_cat, NULL);
   if (setlocale(LC_cat, "new") != 0)
   {
      /* use temporarily changed locale */
      (void)setlocale(LC_cat, oloc);
   }
Most programs will never need this capability.