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:
- Under what circumstance do I need to define the
_UNICODE
macro? - 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
? - 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?
- Why does my sample program above not need to define the
_UNICODE
macro? - 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?