The related problems of TCHAR in VC + +

Source: Internet
Author: User

Objective

What is TCHAR?

Baidu Encyclopedia defines it as follows ":

Because C + + supports two strings, regular ANSI encoding (using "package") and Unicode encoding (using the L "" package), there are two sets of string processing functions, such as: Strlen and Wcslen, which are used to handle two kinds of strings, respectively.

Microsoft has unified the two sets of character sets and their operations, using conditional compilation (through _UNICODE and Unicode macros) to control the actual character set, thus having a string such as _t (""), which corresponds to a function such as _tcslen.in order to store such a universal character, there is a TCHAR:when no _UNICODE macro is defined, TCHAR = Char,_tcslen =strlenWhen a _UNICODE macro is defined, TCHAR = wchar_t, _tcslen = Wcslenwhen we define a Unicode macro, it's equivalent to telling the compiler that I'm going to use the Unicode version. At this time, TCHAR will turn into a wchar_t. Instead of defining a Unicode macro, TCHAR turns into a unsignedchar. This makes it possible to switch the narrow character set very well. TCHAR can be used for double-byte strings, so that the program can be used in Chinese, Japanese and Korean language processing, display. simplifies the programming method. winnt.h header FileMicrosoft provides header files that customize the macros and data structures that may be needed in many windows environments.

I. _t macro definition

The macro definition is located in the Tchar.h header file and is defined as follows:

#define _T (x)       __t (x)

The role of _t macro

The _t macro handles string constants that are enclosed in quotation marks, and the compiler processes the string constants according to the current compilation environment by selecting the appropriate character processing (Unicode, ANSI, and so on).
If you have defined Unicode, then the _t macro will precede the string with an L. At this point _t ("ABCD") is equivalent to L "ABCD", which is a wide string.
If there is no definition, then the _t macro does not precede the string with that l,_t ("ABCD") equivalent to "ABCD".
Text,_text and other macros are defined similarly.
As in the following three statements:

  TCHAR  str1[]   =   TEXT ("str1");     Char   str2[]   =   "str2";     WCHAR  str3[]   =   L ("STR3");   

The first line of code is equivalent to the third line of code when Unicode is defined, and none equals the second row.
But the first two lines of code, whether or not Unicode is defined, generate an ANSI string, and the third line of code always generates a Unicode string.
For the portability of the program, it is recommended to use the first presentation method.
In some cases, however, if a character must be ANSI or Unicode, the latter two methods are used.

Second, LPCSTR and other types

located under winnt.h header fileL indicates long pointer

P indicates that this is a pointer

C means constant const

STR indicates that this variable is a string

LPCSTR 32-bit Pointer, pointing to a constant string
LPSTR 32-bit Pointer, pointing to a string
LPCTSTR 32-bit Pointer, pointing to a constant string. This string can be ported to Unicode and DBCS
LPTSTR 32-bit Pointer, pointing to a string. This string can be ported to Unicode and DBCS

Resources

TCHAR related

The related problems of TCHAR in VC + +

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.