I am learning about win32 api .The TCHAR datatype is used in it for defining stings that are used for registering the class.Why is TCHAR used there?
-
2Does this answer your question? [What are TCHAR strings and the 'A' or 'W' version of Win32 API functions?](https://stackoverflow.com/questions/33836706/what-are-tchar-strings-and-the-a-or-w-version-of-win32-api-functions) – phuclv May 23 '21 at 04:01
-
TCHAR is just a macro that expands as `char` or `wchar_t` depending on the character set settings. It has nothing to do with the real Win32 API which contains 2 versions of A and W that accepts `char` and `wchar_t` respectively – phuclv May 23 '21 at 04:02
-
1You should not use TCHAR these days. Use wchar_t. It's the native character type of Windows and Windows 98 is no longer relevant. – David Heffernan May 23 '21 at 06:25
-
You would only use `TCHAR` if you wanted the same code to compile with both narrow `char` and wide `wchar_t`. This used to be done for Win9x compatibility, which is no longer a concern, or (nowadays) if you wanted dual support for either wide (Win32 native UTF-16) or narrow (e.g. UTF-8) encodings in the same codebase. – dxiv May 23 '21 at 06:42
-
@dxi Compiling the same source for different character sets was a lie back then as it is a lie today. It certainly helped, but in any non-trivial code base you would always see conditional code, identified by manual inspection. It probably isn't very meaningful to have [IsDBCSLeadByte](https://learn.microsoft.com/en-us/windows/win32/api/winnls/nf-winnls-isdbcsleadbyte) in a UTF-16 build for example. – IInspectable May 23 '21 at 07:14
-
@IInspectable Indeed, it's both fragile and painful, and I did not mean to imply that using `TCHAR` alone would make it magically happen. Especially if writing to the Win32 API directly, one would have to basically duplicate the whole CRT mapping [layers](https://learn.microsoft.com/is-is/cpp/c-runtime-library/routine-mappings?view=msvc-160). – dxiv May 23 '21 at 07:28
-
Have a look at: https://learn.microsoft.com/en-us/office/client-developer/outlook/mapi/tchar – Norbert Willhelm May 23 '21 at 11:04
-
[Working with Strings](https://learn.microsoft.com/en-us/windows/win32/learnwin32/working-with-strings) is far more relevant. – IInspectable May 23 '21 at 11:26
-
I just should just write a bot that looks for `TCHAR` in any questions and automatically replies with [this answer](https://stackoverflow.com/a/50572941/104458). Although Mark's answer below is essentially spot on saying the same thing. – selbie May 23 '21 at 20:03
2 Answers
TCHAR
was a crutch created by Microsoft when they were transitioning from code page based characters to Unicode. They wanted an easy way for people to transition their code, so they created a compiler flag that determined whether you were going to do things the old way or the new way. TCHAR
is a macro that expands to char
for the old way or wchar_t
for the new way. Each of the Windows API functions also gets a macro which appends A
for the old style functions or W
for the new functions.
If you're starting a new program, you should definitely use Unicode and forget about all that old baggage. Microsoft is forced to keep using it because they're the kings of backwards compatibility, and there are 20-year-old programs out there that will never be updated to Unicode. That doesn't mean you need to care.

- 299,747
- 42
- 398
- 622
TCHAR is a macro that either expands to char, when UNICOE is not defined, or wchar_t, when UNICODE is defined.
TCHAR is used because of every function there is a A- and W-function. There is also a define for each function, which consists of the function name without W or A. This macro is defined as the W-function, when UNICODE is defined or as the A-function, when UNICODE is not defined.
TCHAR can be used together with the define of each function, consisting only of the function name without A or W.

- 2,579
- 1
- 23
- 33
-
This answers the question *"Why is TCHAR used there?"* literally, but doesn't provide rationale. As such it isn't useful. – IInspectable May 23 '21 at 18:43
-