43

What does the "T" represents in a string. For example _T("Hello").I have seen this in projects where unicode support is needed.What it actually tells the processor

CodeRider
  • 1,750
  • 6
  • 18
  • 34

4 Answers4

46

_T stands for “text”. It will turn your literal into a Unicode wide character literal if and only if you are compiling your sources with Unicode support. See http://msdn.microsoft.com/en-us/library/c426s321.aspx.

MvG
  • 57,380
  • 22
  • 148
  • 276
  • 13
    Technically, `_T()` is only used with the C runtime library, for use with the `_TCHAR` data type. The Win32 equivalent is the `TEXT()` macro for use with the `TCHAR` data type. Both map to `char` or `wchar_t` depending on whether `_UNICODE` and `UNICODE` are defined during compiling, respectively. Both are usually defined/undefined together, so many people tend to interchange them and things usually work. But they are *logically* separate and *should* be treated accordingly. Use `_TCHAR` and `_T()` with C functions. Use `TCHAR` and `TEXT()` with the Win32 API. – Remy Lebeau Oct 13 '15 at 18:48
  • 1
    @RemyLebeau: Now here's the tricky question: Which one to use with MFC/ATL's `CString` type? `CString` is implemented both in terms of the CRT as well as the Windows API. – IInspectable Mar 02 '17 at 17:25
  • 1
    Per the documentation: "***CString is based on the `TCHAR` data type**.*", so use `TEXT()`. – Remy Lebeau Mar 03 '17 at 08:06
17

It's actually used for projects where Unicode and ANSI support is required. It tells the compiler to compile the string literal as either Unicode or ANSI depending on the value of a precompiler define.

Why you would want to do this is another matter. If you want to support Unicode by itself then just write Unicode, in this case L"Hello". The _T() macro was added when you needed to support Windows NT and later (which support Unicode) and Windows 9x/ME (which do not). These days any code using these macros is obsolete, since all modern Windows versions are Unicode-based.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
john
  • 7,897
  • 29
  • 27
15

From MSDN:

Use the _T macro to code literal strings generically, so they compile as Unicode strings under Unicode or as ANSI strings (including MBCS) without Unicode

Tony The Lion
  • 61,704
  • 67
  • 242
  • 415
5

It stands for TEXT. You can peek the definition when using IDE tools:

#define _TEXT(x)    __T(x)

But I would like to memorize it as "Transformable", or "swi-T-ch":

L"Hello"  //change "Hello" string into UNICODE mode, in any case;

_T("Hello") //if defined UNICODE, change "Hello" into UNICODE; otherwise, keep it in ANSI.
JC Ju
  • 189
  • 2
  • 6