Sun Studio 12: C User's Guide

6.9 Internationalization

6.7 Multibyte Characters and Wide Characters introduced the internationalization of the standard libraries. This section discusses the affected library functions and gives some hints on how programs should be written to take advantage of these features. The section only discusses internationalization with respect to the 1990 ISO/IEC C standard. The 1999 ISO/IEC C standard has no significant extension to support internationalization over those discussed here.

6.9.1 Locales



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:


sorting information 


character classification information 


currency printing information 


numeric printing information 


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.

6.9.3 Changed Functions

Wherever possible and appropriate, existing library functions were extended to include locale-dependent behavior. These functions came in two groups:

All ctype.h predicate functions, except isdigit() and isxdigit(), can return nonzero (true) for additional characters when the LC_CTYPE category of the current locale is other than "C". In a Spanish locale, isalpha(’ñ’) should be true. Similarly, the character conversion functions, tolower() and toupper(), should appropriately handle any extra alphabetic characters identified by the isalpha() function. The ctype.h functions are almost always macros that are implemented using table lookups indexed by the character argument. Their behavior is changed by resetting the table(s) to the new locale’s values, and therefore there is no performance impact.

Those functions that write or interpret printable floating values can change to use a decimal-point character other than period (.) when the LC_NUMERIC category of the current locale is other than "C". There is no provision for converting any numeric values to printable form with thousands separator-type characters. When converting from a printable form to an internal form, implementations are allowed to accept such additional forms, again in other than the "C" locale. Those functions that make use of the decimal-point character are the printf() and scanf() families, atof(), and strtod(). Those functions that are allowed implementation-defined extensions are atof(), atoi(), atol(), strtod(), strtol(), strtoul(), and the scanf() family.

6.9.4 New Functions



numeric/monetary conventions 


collation order of two strings 


translate string for collation 


translate string for collation 



