Character and string handling-ansi characters and Unicode characters

Source: Internet
Author: User

We know that the C language uses the char data type to represent a 8-bit ANSI character, and by default when a string is declared in code, the C compiler converts the characters in the string into an array of 8-bit char data types:

// An 8-bit character Char ' A ' ; // An array of 8-bit character and 8-bit terminating zero Char szbuffer["A String";

Microsoft's C + + compiler defines a built-in data type, wchar_t, that represents a 16-bit Unicode (UTF-16) character. The compiler defines this parameter type only if the/zc:wchar_t compiler switch is specified.

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

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

The uppercase letter L before the string notifies the compiler that the string should compile a Unicode string.

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

**pctstr; #define __text (quote) l# #quote#else**pctstr; #define __text (quote) quote#endif#define TEXT (quote) __text (quote)

Using these types and macros to write code, whether using ANSI or Unicode characters, can be compiled as follows:

// If UNICODE Define, a 16-bit character; else an 8-bit character TCHAR C = TEXT ('A'); // If UNICODE Define, an array of 16-bit character; else 8-bit character TCHAR szbuffer[] = TEXT ("A String");

Character and string handling-ansi characters and Unicode characters

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.