TestUser
TestUser

Reputation: 967

How to write a portable c++ code with unicode support?

I have the below program, which tries to print Unicode characters on a console by enabling the _O_U16TEXT mode for the console:

#include <iostream>

#include <fcntl.h>
#include <io.h>

int main()
{
   _setmode(_fileno(stdout), _O_U16TEXT);
   wprintf(L"test\n \x263a\x263b Hello from C/C++\n");
   
   return 0;
}

What is unclear to me is that, I have seen many C++ projects (with the same code running on Windows and Linux) and using a macro called _UNICODE. I have the following questions:

  1. Under what circumstance do I need to define the _UNICODE macro?
  2. Does enabling the _UNICODE macro mean I need to separate the ASCII related code by using #ifdef _UNICODE? In case of the above program, do I need to put any #ifdef UNICODE and the ASCII code processing in #else?
  3. What extra do I need to enable the code to have Unicode support in C/C++? Do I need to link to any specific libraries on Windows and Linux?
  4. Why does my sample program above not need to define the _UNICODE macro?
  5. When I define the _UNICODE macro, how does the code know whether it uses UTF-8, UTF-16 or UTF-32? How do I decide between these Unicode types?

Upvotes: 0

Views: 218

Answers (0)

Related Questions