setlocale


setlocale

Gets or sets locale information

 #include <locale.h> char *setlocale ( int category , const char *locale_name  ); 

The setlocale( ) function allows you to adapt the program to the local conditions of a given regional and cultural environmentcalled a localesuch as clocks and calendars, decimal point and currency symbol characters, and other conventions. The setlocale( ) function returns a pointer to a string that identifies the new locale, or the current locale if you pass the function a null pointer as its second argument.

The locale conventions are classed in categories. You can set the individual categories of the program's locale individually. The header file locale.h defines the following macros to identify each category in the first argument to setlocale( ):


LC_ALL

Includes all the other categories.


LC_COLLATE

Affects the functions strcoll( ), strxfrm( ), wcscoll( ), and wcsxfrm( ).


LC_CTYPE

Affects the character-handling functions (such as isalpha( ), tolower( ), and so on), and the multibyte and wide-character functions.


LC_MONETARY

Affects the monetary format information provided by the localeconv( ) function.


LC_NUMERIC

Affects the nonmonetary numeral format information provided by the localeconv( ) function, and the decimal point used by the printf( ) and scanf( ) functions, and by string conversion functions such as strtod( ).


LC_TIME

Affects the time and date string format produced by the strftime( ) and wcsftime( ) functions.

The second argument to setlocale( ), locale_name, is a pointer to a string that indicates the desired locale,. The permissible locale_name strings are system-dependent, except for two standard values, which are the default locale, "C", and the empty string, "". All locale categories are set to the default locale "C" on program start-up; the "C" locale corresponds to the minimum environment for compiling C programs. If you use the empty string as the locale name, setlocale( ) sets the specified category to the system's native locale. If setlocale( ) is unable to set the desired locale, it returns a null pointer.

If you pass setlocale( ) a null pointer as the locale_name argument, it returns the name of the current locale. You can use this string as the locale_name argument to restore that locale later.

Example

 #define MAX_STRING 80 char name[MAX_STRING]; char locale[MAX_STRING]; char *newlocale; int i; printf( "Who are you? " ); fgets( name, sizeof(name), stdin ); printf( "What is your locale? " ); fgets( locale, sizeof(locale), stdin ); name[ strlen(name) - 1 ] = '\0';       // Chomp off the newlines. locale[ strlen(locale) - 1 ] = '\0'; newlocale = setlocale( LC_CTYPE, locale ); if ( newlocale == NULL )   printf( "Sorry, couldn't change the locale to %s.\n"         "The current locale is %s. ", locale, setlocale( LC_CTYPE, NULL )); else   printf( "The new locale is %s. ", newlocale ); name[0] = toupper( name[0] );    // Force the first letter to uppercase. i = 1; if ( isupper( name[i] ) )        // Is the second letter also uppercase?   {     while ( name[i] != '\0' )    // If so, force all the rest to lowercase.       {         name[i] = tolower( name[i] );         ++i;       }   } printf( "Hello there, %s!\n", name ); 

This program produces output like the following, if the first setlocale( ) call is successful:

 Who are you? sÖrEn What is your locale? de_DE The new locale is de_DE. Hello there, Sören! 

In the locale "de_DE", the isupper( ) function recognized the second letter of sÖrEn as uppercase, and so the Ö and E were changed to lowercase.

If the first setlocale( ) call fails, the output may look like this:

 Who are you? FRÉDÉRIQUE What is your locale? fr_CA Sorry, couldn't change the locale to fr_CA. The current locale is C. Hello there, FrÉdÉrique! 

In the locale "C", the isupper( ) function recognized the R as uppercase, but the tolower( ) function was unable to convert the accented uppercase É.

See Also

The character classification functions, whose names begin with is and isw; the character conversion functions, whose names begin with to and tow; the numeral string conversion functions, whose names begin with strto and wcsto; the locale-sensitive string functions strcoll( ), strxfrm( ), wcscoll( ), and wcsxfrm( ); strftime( ) and wcsftime( )



C(c) In a Nutshell
C in a Nutshell (In a Nutshell (OReilly))
ISBN: 0596006977
EAN: 2147483647
Year: 2006
Pages: 473

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net