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");