Use of VC _ t

Source: Internet
Author: User

When defining strings in VC ++, use _ t to ensure compatibility. VC ++ supports both ASCII and Unicode character types. When _ t is used to ensure conversion from the ASCII encoding type to the Unicode encoding type, the program does not need to be modified.

If you do not plan to upgrade to Unicode in the future, you do not need to upgrade _ T.

_ T ("Hello World ")

In the ANSI environment, it is ANSI. If it is in UNICODE, it is automatically interpreted as a dual-byte string, both Unicode encoding.

These benefits apply to both the ANSI and Unicode environments.

In VC ++, what is the difference between string _ T ("ABC") and a common string "ABC?

_ T ("ABC ")

If Unicode is defined, it is represented as l "ABC", each character is a 16-bit, wide string.

If Unicode is not defined, it is the ASCII "ABC", each character is 8 characters.

Equivalent

# Ifdef _ Unicode

# DEFINE _ T ("ABC") l "ABC"

# Else

# DEFINE _ T ("ABC") "ABC"

# Endif

A character in _ T ("ABC") occupies two bytes, while an English character occupies one byte and a Chinese character occupies two bytes.

1. Add an L to the front of the string:

For example, l "my string" indicates converting an ANSI string to a unicode string, that is, 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 ";

Use of VC _ t

 

 

 

 

From: http://blog.csdn.net/athemon/archive/2008/08/29/2849584.aspx

When defining strings in VC ++, use _ t to ensure compatibility. VC ++ supports both ASCII and Unicode character types. When _ t is used to ensure conversion from the ASCII encoding type to the Unicode encoding type, the program does not need to be modified.

If you do not plan to upgrade to Unicode in the future, you do not need to upgrade _ T.

_ T ("Hello World ")

In the ANSI environment, it is ANSI. If it is in UNICODE, it is automatically interpreted as a dual-byte string, both Unicode encoding.

These benefits apply to both the ANSI and Unicode environments.

In VC ++, what is the difference between string _ T ("ABC") and a common string "ABC?

_ T ("ABC ")

If Unicode is defined, it is represented as l "ABC", each character is a 16-bit, wide string.

If Unicode is not defined, it is the ASCII "ABC", each character is 8 characters.

Equivalent

# Ifdef _ Unicode

# DEFINE _ T ("ABC") l "ABC"

# Else

# DEFINE _ T ("ABC") "ABC"

# Endif

A character in _ T ("ABC") occupies two bytes, while an English character occupies one byte and a Chinese character occupies two bytes.

1. Add an L to the front of the string:

For example, l "my string" indicates converting an ANSI string to a unicode string, that is, 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. The last two methods are used.

 

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. 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.