Char and string types, what should I use? [closed] Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Char and string types, what should I use? [closed] without wasting too much if your time.

The question is published on by Tutorial Guruji team.

At work I’m mainly using C# but I want to learn C++ as well and chars/strings are somewhat confusing. For example, I know that TCHAR can either be a regular char or, if I’m using Unicode, a wchar_t. So, always use TCHAR? But then you find questions like Is TCHAR still relevant?.
Ok, let’s use something else…? So far I’ve only used char and std::string and at this point I have no idea if that was a good approach or not, I’m a bit lost.

What should I use if I’m writing a program which will not be translated to antother language?

What should I use if I’m writing a program which will be translated to antother language?

What should I use if I’m writing a program which will only be used in countries that use Latin characters, which do not have English as their native language (hello ä, ö, ü, ß, æ, Ø, ï…)?

Is there anthing that I can safely ignore because it’s outdated?


Heavily opinionated but based on experience answer

Before I begin, let me state I’ve been working on C++ software for five years, with millions of users globally – in doing so I’ve learned a hell-of-a-lot about how things work in the real world.

The first thing to understand is windows inherently uses it’s (originally homegrown) UTF-16 standard (aka, wide-char). And in doing so makes your life much, much harder.
(almost) every other operating system uses UTF-8. And by that I mean; OS X, *NIX, Android, Ios, pretty much anything you can throw a c++ compiler at.

because of this, do you EVER intend to use your code outside of windows?
If you don’t, there’s no reason not to do it the “windows way”, std::wstring being your best-friend here. You can very easily use .c_str() to get a const wchar_t * (and that implicitly converts into a LPCWSTR). Many of these windows types (such as LPCWSTR, and TCHAR, are actually Macros (aka #define) You can read more on that here.

should you bother with UTF-16 wide characters at all?
It’s very very to think “what if I ignore languages that don’t use a latin alphabet“, trust me when I say, don’t.
Yes, you could use Multibyte characters only, or implicitly call only the A variants of API functions.
However, while this works (and very well), If you support any language beyond Latin-types, you will run into problems. And even if you don’t, users will expect to input in their native language.


English Only, Cross Platform?
In short, There is nothing inherently wrong with using only Ansi 8-bit strings all over windows-programming – it won’t crash the internet, and if you writing something that you know for certain is only going to be used by English-speakers across platforms (software for america?) then I actually recommend changing your project to Multi-Byte, and using std::string for everything, just don’t expect to open A single file with a international filename.
And keep that in mind, if your user-base is in the thousands go utf-8, if its in the tens of thousands, people are going to get mildly angered by not being able to load kanjii-filenames.

International, Windows only
If your software is going to come even close to approaching the internet-border-of-Sweden (where it needs to load a file-name written in Goa’uld), Use std::wstring, use UTF-16, and be happy in windows-only software. To be honest, this is the state of most windows software today.

International, Mac’s are cool? –
Your project manager wants cross-platform software yesterday, it needs to run on Mac and PC – because the users it’s being deployed to are 16% mac users (according to marketing), and it needs to have IME support for Arabic and Japanese.
Tell your project manager you are going to write a wrapper for all your API-calls, it will take a week longer, but will prevent any cross-platform language nonsense, if he doesn’t agree – quit.

Then do just that, Use UTF-8 under the bonnet, and have any API-calls to the windows / mac system handled using a wrapper-class you wrote yourself. Yes it will take some effort and maintenance, but it lets you save a lot of time in the long run.


If you need very complex unicode support, check out the ICU library, OSX uses this under the hood!)
Learn to use BOOST – the filesystem support alone makes cross-platform C++ development much, much faster

We are here to answer your question about Char and string types, what should I use? [closed] - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji