C++ - How to write Unicode text to the console

Two methods to write Unicode text to the console are:

  1. wprintf() with _setmode()
  2. WriteConsoleW()

Note: After you call _setmode(_O_U16TEXT), any calls to printf() (non-wide) will fail with an assertion.


  #include "stdafx.h"
  #include <io.h> // for _setmode
  #include <fcntl.h> // for _O_U16TEXT
  #include <windows.h> // for WriteConsoleW
  
  int main()
  {
      // Copyright symbol, pound symbol, Unicode code point U+03A3 Greek Capital Letter Signma Σ
      wchar_t wide[] = L"\r\nCopyright © £ \u03a3";
      
      // method 1
      _setmode(_fileno(stdout), _O_U16TEXT);
      wprintf(wide);
      
      // method 2
      auto handle = GetStdHandle(STD_OUTPUT_HANDLE);
      WriteConsoleW(handle, wide, wcslen(wide), NULL, NULL);
      
      return 0;
  }
  

Program output:

  Copyright © £ Σ
  Copyright © £ Σ

Ads by Google


Ask a question, send a comment, or report a problem - click here to contact me.

© Richard McGrath