What is the point of TCHAR?

lecture 63
const TCHAR HW[]=TEXT(cake)'
what does `TCHAR do ? i tried to google it but there is no clear answer !!

It is a type alias to be the correct char type on the target platform for Unicode. For example on Windows that’s wchar_t so TCHAR is a type alias for wchar_t on Windows. On Linux it’s a type alias for char

Not exact code, just to give you an example

#if _WIN32 // if Windows
using TCHAR = wchar_t;
using TCHAR = char;

sorry but i really don’t understand these terms i am new to C++
can you explain what TCHAR do to beginner ?
i really tried to google TCHAR but all the answers are very complex for noobies :frowning:

You don’t need to know anything beyond “it’s the correct char type for the platform that’s being targeted”

1 Like

When C (the language C++ is based on) was designed in 1970, computers only used a specific set of characters, called ANSI characters, which is a number between 0 and 255, to represent all the characters of the American alphabet. Later on, it became a necessity to represent characters from other alphabets, such as the Greek alphabet, the Chinese pictograms, the Japanese kanjis, etc. Therefore, C++ introduced a new type called the “wide character”, that would hold more than 32,000 possible characters, thus there’s room for every character from every language mankind has created. This wide character type is represented by the keyword wchar_t.

Now, when wchar_t became available, lots of C and C++ programs were already made so in order to facilitate the transition, several platforms (chiefly, Microsoft Windows) created options to compile code targeting normal ANSI characters (char) or wide characters (wchar_t). To make it easy for developers, they created the TCHAR macro, which will resolve to char when you’re compiling with ANSI option, or to wchar_t otherwise. That’s why we use TCHAR.

Incidentaly, the macro TEXT is used for the same purpose. You assign a string to a TCHAR by using quotes (e.g. char* sz = “Hello”), but you should use L"" for wide characters (e.g. wchar_t* sz = L"Hello"). The TEXT macro resolves that for you, so you can be oblivious to what type of compilation you are doing.

So that’s the reason why it exists, its concept is complex because of all the history, and what I just wrote is a simplification, but hopefully it’ll help you to understand a wee bit more about an already complex language such as C++. Cheers!


This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms