Use of setlocale ()/_ wsetlocale () function

Source: Internet
Author: User

In the multi-byte character-wide conversion function provided by the C Runtime Library: mbstowcs ()/wcstombs (), the global variable locale (locale encoding) must be used ), to specify the encoding type of multi-byte characters

1. functions:

Used to define global variables: locale (locale encoding)

Header file:

Setlocale <Locale. h> ANSI, Win 95, Win NT
_ Wsetlocale <Locale. h> or <wchar. h>

2. prototype:

Char * setlocale (int Category, Const char *Locale );

Wchar_t * _ wsetlocale (int Category, Const wchar_t *Locale );

3. parameters:

1> category

Specify the range to be affected by the global variable locale. Macro and the corresponding impact scope are as follows:

Lc_all

All categories, as listed below

Lc_collate

TheStrcoll, _ stricoll, wcscoll, _ wcsicoll,AndStrxfrmFunctions

Lc_ctype

The character-handling functions (character tIsdigit,Isxdigit,Mbstowcs, AndMbtowc, Which are unaffected)

Lc_monetary

Monetary-formatting information returned byLocaleconvFunction

Lc_numeric

Decimal-point character for the formatted Output Routines (suchPrintf), For the data-conversion routines, and for the nonmonetary-formatting information returnedLocaleconv

Lc_time

TheStrftimeAndWcsftimeFunctions

2>Locale 

Name of locale

If the value is a Null String "" (Note: It is not null), locale uses the same encoding method as the local environment (it should be useful in localization );

If the value is "C", it indicates that all character types are expressed in one byte (the value is less than 256)

Locale in simplified Chinese is named "CHS"

4. Return Value:

If both locale and category are correct, strings related to the specified locale and category are returned;

If locale or category is incorrect, a null pointer is returned. The current locale global variable will not change.

Sample Code:

Obtain the local language:

Void main () {char * locallanguage = setlocale (lc_all, ""); If (locallanguage = NULL) {printf ("failed to get the local language type \ n "); return;} printf ("locale language is % s \ n", locallanguage );}

The output is as follows:

Locale language is chinese_people 'S republic of china.936

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.