52

I've been looking at the command line generated by Visual Studio, and for one of my project it defines two symbols: _UNICODE and UNICODE. Now if I understand this document this rather old document, the _UNICODE symbol is a VC++ thing that causes certain standard functions to use wchar_t instead of char in their interfaces.

But what does the UNICODE without an underscore mean?

Joey
  • 344,408
  • 85
  • 689
  • 683
Adrian Ratnapala
  • 5,485
  • 2
  • 29
  • 39
  • Maybe use functions like CreateWindowW instead of CreateWindowA when UNICODE defined, so UNICODE is for WinApi, _UNICODE is for C run time library – Blablablaster Oct 31 '11 at 11:26

3 Answers3

69

Raymond Chen explains it here: TEXT vs. _TEXT vs. _T, and UNICODE vs. _UNICODE:

The plain versions without the underscore affect the character set the Windows header files treat as default. So if you define UNICODE, then GetWindowText will map to GetWindowTextW instead of GetWindowTextA, for example. Similarly, the TEXT macro will map to L"..." instead of "...".

The versions with the underscore affect the character set the C runtime header files treat as default. So if you define _UNICODE, then _tcslen will map to wcslen instead of strlen, for example. Similarly, the _TEXT macro will map to L"..." instead of "...".

Looking into Windows SDK you will find things like this:

#ifdef _UNICODE
#ifndef UNICODE
#define UNICODE
#endif
#endif
Roman R.
  • 68,205
  • 6
  • 94
  • 158
  • 1
    The correct link is: https://devblogs.microsoft.com/oldnewthing/20040212-00/?p=40643, the old one is dead (thanks MS)… Also, the "Suggested edit queue is full", that’s why I’m writing a comment. – mrBen Jul 13 '22 at 08:00
31

In a nutshell,

UNICODE is used by Windows headers,

whereas

_UNICODE is used by C-runtime/MFC headers.

rstackhouse
  • 2,238
  • 24
  • 28
AminM
  • 1,658
  • 4
  • 32
  • 48
22

Compiler vendors have to prefix the identifiers in their header files with an underscore to prevent them from colliding with your identifiers. So <tchar.h>, a compiler header file, uses _UNICODE. The Windows SDK header files are compiler agnostic, and stone-cold old, it uses UNICODE without the underscore. You'll have to define both.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536