Char, wchar_t, tchar, _ T (), L, macro _ T, text, _ text, l

Source: Internet
Author: User

CHAR: single-byte variable type, which can contain a maximum of 256 characters,

Wchar_t: Type of a wide BYTE variable, used to represent Unicode characters,

It is actually defined in <string. h>: typedef unsigned short wchar_t.

To enable the compiler to recognize Unicode strings, you must add an "L" to the front and define the wide byte type as follows:

Wchar_t c = 'a ';
Wchar_t * P = l "Hello! ";
Wchar_t A [] = l "Hello! ";

Each variable of the wide byte type occupies 2 bytes. Therefore, sizeof (A) = 14 of the preceding array

Tchar/_ T ():
IfProgramIncluding both ANSI and Unicode encoding, including the header file tchar. h. Tchar is the macro defined in this header file. It is defined:
_ UNICODE: typedef wchar_t tchar;
No _ UNICODE: typedef char tchar;

# Ifdef Unicode
Typedef char tchar;
# Else
Typede wchar_t tchar;
# Endif
_ T () is also the macro defined in this header file, which is defined:
_ UNICODE: # DEFINE _ T (x) L # x
_ UNICODE: # DEFINE _ T (x) X Not Defined
NOTE: If tchar is used in the program, you should not use the ANSI strxxx function or the Unicode wcsxxx function. Instead, you must use the _ tcsxxx function defined in tchar. h.

Take the strcpy function as an example to summarize:

Code
// If you want to use an ANSI string, use this method:
Char Szstring [ 100 ];
Strcpy (szstring, " Test " );
// If you want to use a unicode string, use this set:
Wchar_t szstring [ 100 ];
Wcscpy (szstring, l " Test " );
// If you want to compile an ANSI or Unicode string by defining a _ Unicode macroCode:
Tchar szstring [ 100 ];
_ Tcscpy (szstring, _ text ( " Test " ));

Csdn: superarhow: Do not use tchar and _ t any more! He analyzed the cause and concluded: If you are starting a new project, you must withstand the pressure and use Unicode encoding! Remember! You only need to train your team members for 10 minutes. Remember to replace strcpy with wcscpy, sprintf with swprintf, and add L before the constant! It will not take you a lot of time to bring you stability and security! Believe me, that's right !!

1. Add an L to the front of the string:
For example, l "my string" indicates ANSI string to Unicode Is that each character occupies two bytes.
Strlen ("ASD") = 3;
Strlen (L "ASD") = 6;
2. The _ t macro can enclose a string enclosed by quotation marks. Based on your environment settings, the compiler selects an appropriate (UNICODE or ANSI) Character Processing Method Based on the compiling target environment.
If Unicode is defined, the _ t macro adds an L to the front of the string. In this case, _ T ("ABCD") is equivalent to L "ABCD", which is a wide string.
If not defined, the _ t Macro will not add the L before the string. _ T ("ABCD") is equivalent to "ABCD"
3. Text, _ text and _ t are the same
See the following three statements:
Tchar szstr1 [] = text ("str1 ");
Char szstr2 [] = "str2 ";
Wchar szstr3 [] = L ("str3 ");
The first sentence is interpreted as the third sentence when Unicode is defined. If no definition is made, it is the second sentence.
However, whether Unicode is defined or not, an ANSI string is generated, and the third sentence is always a unicode string.
We recommend that you use the first representation for program portability.
However, in some cases, a character must be ANSI or Unicode, and the last two methods are used.

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.