Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In C++ when to use WCHAR and when to use CHAR

Tags:

c++

unicode

I have a question:

Some libraries use WCHAR as the text parameter and others use CHAR (as UTF-8): I need to know when to use WCHAR or CHAR when I write my own library.

like image 865
user2179256 Avatar asked Apr 17 '14 15:04

user2179256


People also ask

What is the difference between Wchar and char?

The type unsigned char is often used to represent a byte, which isn't a built-in type in C++. The wchar_t type is an implementation-defined wide character type.

What does Wchar stand for?

This document sets out the walking, cycling and horse-riding assessment and review (WCHAR) process for highway schemes on motorways and all-purpose trunk roads.

How many bytes is Wchar?

Just like the type for character constants is char, the type for wide character is wchar_t. This data type occupies 2 or 4 bytes depending on the compiler being used.

What is the size of wchar_t?

the default size of wchar_t is 4 bytes.


1 Answers

Use char and treat it as UTF-8. There are a great many reasons for this; this website summarises it much better than I can:

http://utf8everywhere.org/

It recommends converting from wchar_t to char (UTF-16 to UTF-8) as soon as you receive it from any library, and converting back when you need to pass strings to it. So to answer your question, always use char except at the point that an API requires you to pass or receive wchar_t.

like image 75
Ben Hymers Avatar answered Nov 02 '22 23:11

Ben Hymers