TCHAR is what it is when your character is set.
For example:
When the program is compiled to ANSI, TCHAR is equivalent to CHAR
When the program compiles to UNICODE, TCHAR is equivalent to WCHAR
Char: Single-byte variable type, representing up to 256 characters
wchar_t: Wide-byte variable type, used to represent Unicode characters
It is actually defined in <string.h>: typedef unsigned short wchar_t.
In order for the compiler to recognize Unicode strings, you must define a wide-byte type by adding an "L" in front of the following:
wchar_t c = ' A ';
wchar_t * p = L "hello!";
wchar_t a[] = L "hello!";
Where the wide byte type each variable occupies 2 bytes, so the above array A's sizeof (a) = 14
TCHAR/_t ():
If you include both ANSI and Unicode encoding in your program, you need to include the header file Tchar.h.
TCHAR is a macro defined in the header file, and it is defined as whether you define a _UNICODE macro:
Defines the _unicode:typedef wchar_t TCHAR;
No _unicode:typedef char TCHAR defined;
#ifdef UNICODE
typedef char TCHAR;
#else
Typede wchar_t TCHAR;
#endif
_t () is also a macro defined in the header file, depending on whether the _UNICODE macro is defined as
Defines the _unicode: #define _t (x) l# #x
Not defined _unicode: #define _t (x) x
Note: If you use TCHAR in your program, you should not use the ANSI strxxx function or the Unicode wcsxxx function, but you must use the _TCSXXX function defined in Tchar.h
First , add an L function before the string:
such as L "my string" means that theANSI string converted to Unicodeis a string that takes two bytes per character.
strlen ("asd") = 3;
strlen (L "ASD") = 6;
The _t macro can enclose a quoted string, depending on your environment settings, so that the compiler chooses the appropriate (Unicode or ANSI) character processing based on the compiled target environment
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"
third, Text,_text and _t the same
as in the following three statements:
TCHAR szstr1[] = TEXT ("str1");
char szstr2[] = "STR2";
WCHAR szstr3[] = L ("Str3");
The first sentence, when defined as Unicode, is interpreted as a third sentence, which is equal to the second sentence when it is not defined.
but two words whether or not Unicode is defined produces an ANSI string, and the third sentence always generates a Unicode string.
for the portability of the program, it is recommended to use the first presentation method.
However, in some cases, a character must be ANSI or Unicode, then use the latter two methods.
"Turn" TCHAR