Tchar, Unicode, Char, wchar_t! (Go !)

Source: Internet
Author: User

When we build a project together, we often find that some people love to use standard ANSI functions such as strcpy, and some love to use the _ txxxx function. This problem has been very confusing. In order to unify, it is necessary to clarify the ins and outs.

To understand these functions, you must write several character types. Not to mention Char. Let's talk about wchar_t first. Wchar_t is the data type of Unicode characters. It is actually defined in <string. h>:
Typedef unsigned short wchar_t;
You cannot use ansi c string functions such as strcpy to process wchar_t strings. You must use functions prefixed with WCS, such as wcscpy. To enable the compiler to recognize Unicode strings, you must add an "L" to the front. For example:
Wchar_t * sztest = l "this is a unicode string .";

Next let's take a look at tchar. If you want to compile both ANSI and UnicodeSource codeTo include tchar. h. Tchar is a macro defined in it. It is defined as Char or wchar_t based on whether _ Unicode macro is defined. If you use tchar, you should not use the ANSI strxxx function or the Unicode wcsxxx function. Instead, you must use the _ tcsxxx function defined in tchar. h. In addition, to solve the problem with "L" mentioned earlier, tchar. h defines a macro: "_ text ".

take the strcpy function as an example to summarize:
. if you want to use an ANSI string, use this method:
char szstring [100];
strcpy (szstring, "test");
. if you want to use Unicode strings, use this set:
wchar_t szstring [100];
wcscpyszstring, l "test");
. if you want to compile an ANSI or Unicode string Code by defining a _ Unicode macro:
tchar szstring [100];
_ tcscpy (szstring, _ text ("test");

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.