BOOL vs. VARIANT_BOOL vs. BOOLEAN vs. bool

Date:December 22, 2004 / year-entry #431
Tags:code
Orig Link:https://blogs.msdn.microsoft.com/oldnewthing/20041222-00/?p=36923
Comments:    29
Summary:Still more ways of saying the same thing. Why so many? Because each was invented by different people at different times to solve different problems. BOOL is the oldest one. Its definition is simply typedef int BOOL; The C programming language uses "int" as its boolean type, and Windows 1.0 was written back when C was...

Still more ways of saying the same thing. Why so many?

Because each was invented by different people at different times to solve different problems.

BOOL is the oldest one. Its definition is simply

typedef int BOOL;

The C programming language uses "int" as its boolean type, and Windows 1.0 was written back when C was the cool language for systems programming.

Next came BOOLEAN.

typedef BYTE  BOOLEAN;

This type was introduced by the OS/2 NT team when they decided to write a new operating system from scratch. It lingers in Win32 in the places where the original NT design peeks through, like the security subsystem and interacting with drivers.

Off to the side came VARIANT_BOOL.

typedef short VARIANT_BOOL;
#define VARIANT_TRUE ((VARIANT_BOOL)-1)
#define VARIANT_FALSE ((VARIANT_BOOL)0)

This was developed by the Visual Basic folks. Basic uses -1 to represent "true" and 0 to represent "false", and VARIANT_BOOL was designed to preserve this behavior.

Common bug: When manipulating VARIANTs of type VT_BOOL, and you want to set a boolean value to "true", you must use VARIANT_TRUE. Many people mistakenly use TRUE or true, which are not the same thing as VARIANT_TRUE. You can cause problem with scripting languages if you get them confused. (For symmetry, you should also use VARIANT_FALSE instead of FALSE or false. All three have the same numerical value, however. Consequently, a mistake when manipulating "false" values is not fatal.)

Newest on the scene is bool, which is a C++ data type that has the value true or false. You won't see this used much (if at all) in Win32 because Win32 tries to remain C-compatible.

(Note that C-compatible isn't the same as C-friendly. Although you can do COM from C, it isn't fun.)


Comments (29)
  1. Pedro says:

    It’s funny to see this posting above one called "Sometimes people don’t like it when you enforce a standard". Selective soapboxing?

  2. Dan Golick says:

    The important thing to remember is not to compare any of the bool values to true, (TRUE, or VARIANT_TRUE)

    Instead of:

    if (x == TRUE)

    use:

    if (x)

    or:

    if (x != VARIANT_FALSE)

    Your guarnateed that someone will put something other than 1 (-1) in as a true value.

  3. Dennis Jackson says:

    People wonder why simple bugs can slip into any software so easily. if the foundations are a mess, what do you expect the top layers to look like?

  4. Raymond Chen says:

    Pedro: The great thing about standards is there are so many to choose from! C uses one method for booleans, BASIC another, C++ yet another. If you want to complain, complain to the language designers for continually inventing new ways of expressing booleans.

    Dennis Jackson: Or you can use a language like VB that isolates the language interop issues. If you choose a low-level language like C or C++ then you are effectively volunteering to handle all the interop issues yourself.

  5. asdf says:

    C99 has a real boolean type called _Bool that behaves pretty much like C++’s bool. Though you need stdbool.h (or your own macros) to define true and false.

  6. Raymond Chen says:

    Stuart Ballard: True there are still more boolean-ish types, but I tried to stick to the ones you would find in C/C++ code.

  7. You could still find System.Boolean in Managed C++ (or it’s Whidbey incarnation as C++/CLI)…

  8. Rich says:

    … to Raymond, probably the hardest-working technical blogger on the planet. Long may he continue to blog!

  9. Don’t forget CORBA’s "Boolean" type. That turns up alot in C++, or at least it does where I work.

    If you’re going to count Java, there’s also java.lang.Boolean. Nasty primitive/object dichotomy…

  10. Scott says:

    So just to summarize.

    BOOL != BOOLEAN != VARIANT_BOOL != bool

    is that right? ;)

  11. All three have the same numerical value,

    > however. Consequently, a mistake when

    > manipulating "false" values is not fatal.

    That’s why we also have S_FALSE :)

  12. Eric Lippert says:

    I discussed the reason why VB/VBScript use -1 for VARIANT_TRUE instead of 1 here:

    http://blogs.msdn.com/EricLippert/archive/2004/07/15/184431.aspx

  13. Norman Diamond says:

    Actually among all of the C/C++ boolean types, the oldest is…

    int

    (without being typedef’ed to any leaky abstracting nicer-looking identifiers, even if the abstraction leakage that was imported by overloading boolean meanings onto int wasn’t enough already).

    By the way, after gradually learning how Microsoft isn’t as monolithic internally as it is in crushing the marketplace, I wonder how it avoided one boolean problem that was common elsewhere? The problem came because everyone knew it would be prettier to name their booleans boolean instead of int, and everyone knew what true and false meant, so everyone did define boolean and true and false. And if any C source file had to #include two header files that were written by such clever people, then the chances were high of having two incompatible definitions for boolean and/or true and/or false. Of course the definitions would all have the same effects if any one of them had been chosen, it didn’t matter which one, but a declaration as an enum isn’t compatible with a typedef of int, a #define to 1 isn’t compatible with a #define to !false, etc.

  14. You forgot "boolean" (invented by Sun when they created Java as a brand-new language with C-like syntax) and System.Boolean (the .NET Framework BCL boolean type) and Boolean (the way you’d normally actually *type* System.Boolean because you normally have the System namespace in scope) and "bool" (in it’s C# incarnation as an alias for System.Boolean).

  15. Stuart Ballard – Yeah, but as he said, he tried to stick to the ones you’d find in C/C++ code. *ahem*

    I’ve still yet to find a good reason to actually use managed C++.

  16. Andy says:

    "I’ve still yet to find a good reason to actually use managed C++" – Simon Cooke

    Simon I posted a response here so this post doesn’t get to off topic.

    http://www.cadencoding.net/blogs/users/cornbread/permalink.aspx?id=108

    Raymond I love these historical posts of yours. I can’t get enough of them. Have you ever considered gathering up the "history of the many strange and forgotten things about windows" and putting them in a book. I for one would buy it in a heart beat.

  17. Andy says:

    Thank you for the link Miss Fox. It explains a lot.

    Well in that case keep on rocking Raymond I really like your blog.

  18. Quoth the Raymond:

    > The C programming language uses "int" as its boolean type, and Windows 1.0 was written back when C was the cool language for systems programming.

    Funny thing to say – there was a lot of assembly in the world at the time, and in particular in Windows 1.0, no? I still occasionally work with a little assembly for Windows 98/Me drivers.

    OTOH, I’d go out on a limb and say that C *is* the cool language for systems programming, at least judging by market share: Windows NT/2k/xp/2k3, Linux, *BSD, Mac OSX, … not much left.

  19. Kent says:

    To all those saying "don’t forget such and such a language" . . .

    Sure, most languages will have a boolean type. Some languages (eg. C#) will have several ways of using said type (bool, System.Boolean). However, I think Raymond’s point is that – when coding in C/C++ – you have several choices that *do not amount to the same thing*.

    This has long been a point of confusion for me when coding in C++ as I am predominantly a C# coder. I try to use bool but then I find myself using other forms (eg. BOOL) because of my reliance on third-party libraries.

    I guess the trick is to choose one and stick to it in your public APIs, converting other forms to your chosen one.

    Thanks for clearing this up Raymond.

  20. roy says:

    Here’s one for you. If you run Windows XP French OS, and you are doing some VBScript, you get back "vrai" or "faux" (string)!!!

    I’m not kidding. I totally forget how we solved it, probably dealt with the numerical value (0,1) rather than having it check for:

    If X = "true" Then …

    This failed because the returned string was "vrai."

    Have any of you that use a different language OS encountered some differences like that?

    Hey raymond, what’s true or false in swedish :-P?

    Btw, that bug took me a whole day to find because I code in English, and keep my French on the side. Didn’t see that one coming!

  21. Dave says:

    I know VB(script) does a lot of conversions for you, but a two-way conversion between boolean and string values? I can see converting it to string for output, then the localization makes sense. But on the input side (string "false","False","FAUX" ==> boolean false)? Why not make "no", "never", and "not-gonna-do-it" map to false as well? :)

    My candidate for most unexpected value goes to S_FALSE, ASSERT(S_FALSE==TRUE) always succeeds.

  22. Norman Diamond says:

    12/23/2004 1:14 PM roy

    > Have any of you that use a different

    > language OS encountered some differences

    > like that?

    One answer in Windows was the first public version of the Microsoft Baseline Security Analyzer. For all four of the zones that Internet Explorer groups sites into, MBSA recommended setting security to Low. And the maker called themselves a security company, so I sent a rather nasty note about that. If they can’t figure out what zone they’re talking about, they’re far better off saying so and/or defaulting security to High.

    In the Linux world there are problems like this all the time. It is frequently recommended that root’s locale should be US English regardless of the locale used by everyone else, so that most scripts will see things the way that the script writers expected. Of course Linux includes quite a lot of multilingual support but it seems that scripts are one place where there haven’t been a lot of volunteers.

  23. Norman Diamond says:

    In VB you can do this:

    If SomeBooleanVariable = flase then



    End If

    It works because flase is undeclared, so it is intialized to a zero of some sort, the same value as False.

    I just wish it were so easy to decipher the rest of the program that used it…..

  24. Jan says:

    Microsoft also invented special kind of BOOL value with three distinct meanings. See for instance GetMessage() API.

  25. Johann says:

    roy wrote:

    > Hey raymond, what’s true or false in swedish :-P?

    "sant" and "falskt"

Comments are closed.


*DISCLAIMER: I DO NOT OWN THIS CONTENT. If you are the owner and would like it removed, please contact me. The content herein is an archived reproduction of entries from Raymond Chen's "Old New Thing" Blog (most recent link is here). It may have slight formatting modifications for consistency and to improve readability.

WHY DID I DUPLICATE THIS CONTENT HERE? Let me first say this site has never had anything to sell and has never shown ads of any kind. I have nothing monetarily to gain by duplicating content here. Because I had made my own local copy of this content throughout the years, for ease of using tools like grep, I decided to put it online after I discovered some of the original content previously and publicly available, had disappeared approximately early to mid 2019. At the same time, I present the content in an easily accessible theme-agnostic way.

The information provided by Raymond's blog is, for all practical purposes, more authoritative on Windows Development than Microsoft's own MSDN documentation and should be considered supplemental reading to that documentation. The wealth of missing details provided by this blog that Microsoft could not or did not document about Windows over the years is vital enough, many would agree an online "backup" of these details is a necessary endeavor. Specifics include:

<-- Back to Old New Thing Archive Index