Character and string processing-ANSI and Unicode characters,-ansiunicode

Source: Internet
Author: User

Character and string processing-ANSI and Unicode characters,-ansiunicode

We know that the C language uses the char data type to represent an eight-digit ANSI character. By default, when a string is declared in the code, the C compiler converts the characters in a string to an array consisting of eight-bit char data types:

// An 8-bit characterchar c = 'A';// An array of 99 8-bit character and 8-bit terminating zerochar szBuffer[100] = "A String";

Microsoft's C/C ++ compiler defines a built-in data type wchar_t that represents a 16-bit Unicode (UTF-16) character. This parameter type is defined only when the/Zc: wchar_t compiler switch is specified by the compiler.

The methods for declaring Unicode characters and strings are as follows:

// A 16-bit characterwchar_t c= L'A';// An array up to 99 16-bit characters and a 16-bit terminating zerowchar_t szBuffer[100] = L"A String";

The uppercase letter L before the string notifies the compiler that the string should be compiled into a Unicode string.

In addition, you can use ANSI or Unicode characters/strings to compile the code. WinNT. h defines the following types and macros:

#ifdef UNICODEtypedef WCHAR TCHAR, *PTCHAR, PTSTR;typedef CONST WCHAR *PCTSTR;#define __TEXT(quote) L##quote#elsetypedef CHAR TCHAR, *PTCHAR, PTSTR;typedef CONST CHAR *PCTSTR;#define __TEXT(quote) quote#endif#define TEXT(quote) __TEXT(quote)

Use these types and macros to write code. Both ANSI and Unicode characters can be compiled as follows:

// If UNICODE define, a 16-bit character; else an 8-bit characterTCHAR c = TEXT('A');// If UNICODE define, an array of 16-bit character; else 8-bit characterTCHAR szBuffer[100] = TEXT("A String");

 

Related Article

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.