C language setlocale () function Detailed

setlocale function

Function prototype: char * setlocale (int category, const char * locale);

setlocale located in the header file , setlocale () function may be used to program the current area settings (setting local, regional settings), can also be used to obtain the current program area setting information , requires two parameters of setlocale

 

The first parameter category:

Used to set the scope of the geographical setting. Geographical settings contain the contents of multiple aspects of date formats, number formats, currency formats, character handling, compare, and so on, the current regional settings may affect only certain aspects of the content, it can also affect all content,

Not just the value of the category set, the macro must <locale.h> defined

 

 

The second parameter locale:

 

setlocale function return values:

If setlocale () performed successfully , then returns a pointer to a string, the string contains the name of the current local settings. In other words, setlocale () will return the name of the current regional settings

If setlocale () execution failed (e.g. locale is designated name does not exist, will lead to failure area disposed), then it returns NULL pointer

 

If no other parameters and other regions with setlocale function program, then the program runs locale C is initialized to the default locale, which uses character codes are all part of the common local ANSI character set encoding is used to write the C language source code the minimum character set,

Setlocale below with reference to geographical settings currently in use give

char *p;

p = setlocale(LC_ALL, NULL);
printf("%s\n", p);

operation result:

From the above you can see the current regional settings used by the compiler is C, if you want to output Chinese characters with a wide, but still there will be problems with the default regional settings (C), the following setlocale try to resolve the wide-character output of Chinese problem

wchar_t SS [ 20 is ];
 char * P; 

P = the setlocale (the LC_ALL, NULL); 
SS [ 0 ] = _T ( ' in ' ); 
SS [ . 1 ] = _T ( ' text ' ); 
SS [ 2 ] = _T ( ' \ 0 ' ); 
the wprintf (L " % LS \ n- " , SS); 

the printf ( " Regional settings:% S \ n- " , P);

operation result:

From the above results it can be seen running 'the' text 'word was not correct output, because the default locale is initialized to C, which is the character encoding used for all local public portion ANSI character set encoding is C language source code for writing the smallest character set , the area below with reference to setlocale to Simplified Chinese, the modified code as follows:

wchar_t ss[20];
char *p;

p = setlocale(LC_ALL, "");
ss[0] = _T('');
ss[1] = _T('');
ss[2] = _T('\0');
wprintf(L"%ls\n", ss);

printf("地域设置:%s\n", p);

operation result:

 

Because the operating system is the Chinese environment, so the above code can be changed to the effect is the same

Guess you like

Origin www.linuxidc.com/Linux/2019-08/159759.htm