Character and string processing (ANSI and Unicode characters) in C language _c language

Source: Internet
Author: User

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

Copy Code code as follows:

An 8-bit character
char c = ' A ';
An array of 8-bit character and 8-bit terminating zero
Char szbuffer[100] = "A String";

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

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

Copy Code code as follows:

A 16-bit Character
wchar_t c= L ' A ';
An array up to 16-bit characters and a 16-bit terminating zero
wchar_t szbuffer[100] = L "A String";

Uppercase letters before strings L 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 enable it to compile. WinNT.h defines the following types and macros:

Copy Code code as follows:

#ifdef UNICODE
typedef WCHAR TCHAR, *ptchar, ptstr;
typedef CONST WCHAR *pctstr;
#define __text (quote) l# #quote
#else
typedef CHAR TCHAR, *ptchar, ptstr;
typedef CONST CHAR *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:

Copy Code code 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[100] = TEXT ("A String");

The above mentioned is all Neri of this article, hope everybody can like.

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.