29

In many programs, I see statements with the identifiers FALSE and false. Is there any difference between them in the context of C++?

Also in some programs, I saw bool and somewhere BOOL. What is the difference between both of these?

Can anyone explain the difference between these identifiers to me? Thanks.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
RidaSana
  • 553
  • 2
  • 6
  • 15

6 Answers6

32

If you've included windows.h, then FALSE is #defined as 0. TRUE is #defined as 1. It is not a Boolean type, it's an integer type. Windows also defined the type BOOL, which is actually an integer type designed to accomodate the values TRUE and FALSE.

The relevant sections of code in windows.h look something like this:

#ifdef FALSE
#undef FALSE
#endif
#define FALSE 0

#ifdef TRUE
#undef TRUE
#endif
#define TRUE  1

typedef int    BOOL,    *PBOOL,    *LPBOOL;

Windows was originally written in C, as were all of the original Windows applications. At that time, C did not have a Boolean type defined by the standard. Instead, developers were forced to define their own Boolean constants. To prevent the possible mess that could cause, Microsoft decided on a standard that would be used for all Windows applications.

If you're writing Windows applications, you may still need to use the TRUE and FALSE identifiers. Several of the functions defined in windows.h either accept Boolean parameters (actually integers!) typed as BOOL or return values of such type. C++, being strongly typed, may complain if you coerce these to the Boolean type defined in the language's standard. For consistency, I use the old identifiers in all of my calls to Windows API functions, and the true and false keywords now defined by the standard in all of the methods that I write myself.

There's really no reason not to use FALSE as it is defined in windows.h. But you shouldn't define it yourself in a proprietary header file, as others have mentioned, because there's no guarantee that the definition will remain constant over time. That could cause some pretty silly bugs, the type that qualify to appear on the Daily WTF.

Mat's answer is 100% correct that neither TRUE or FALSE are defined by the C or C++ standards. However, they are strictly defined by the Windows API and guaranteed not to change. Using them will not produce truly cross-platform, strictly standards-compliant C++ code, but that's rarely a concern when writing Windows apps, as neither will calling any of the other functions in windows.h.

Community
  • 1
  • 1
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
  • @cody: yes i am using window application... so i am using FALSE and TRUE.. its working with bool type... – RidaSana Apr 14 '11 at 13:16
  • @Cody: what about second part of the question about bool and BOOL? – RidaSana Apr 14 '11 at 13:21
  • 1
    @Miss: I added that in an update to my answer. Succinctly, `bool` is the Boolean type defined in the C++ standard. It works with the also lower-case `true` and `false` keywords. `BOOL` is an integer type, defined by `windows.h`, designed to work with the also upper-cased `TRUE` and `FALSE`. Easy to remember, as the cases match for each group. – Cody Gray - on strike Apr 14 '11 at 13:23
  • 1
    @Miss: Naw, I don't think so. The `BOOL` type is only defined to work with `TRUE` and `FALSE`. It's all part of `windows.h`. I'll be happy to explain any further if you didn't understand my last comment. :-) – Cody Gray - on strike Apr 14 '11 at 13:23
  • @Cody, yes i did not understand your words, "Naw, I don't think so".. what are you talking about here now... – RidaSana Apr 14 '11 at 13:26
  • 1
    @Miss: It's a colloquial way of saying "no". I was replying to your comment about creating a new question about the difference between `bool` and `BOOL`. I guess you've deleted that comment now, after you saw the one I'd posted before it. – Cody Gray - on strike Apr 14 '11 at 13:27
  • @Cody and what do you mean by "*PBOOL, *LPBOOL;" in the code.. typedef int BOOL i understand but i do't understand this: "*PBOOL, *LPBOOL;" .. – RidaSana Apr 14 '11 at 13:28
  • 1
    @Miss: Ehh, probably not important. That also defines a *pointer* to the `BOOL` type (`*PBOOL`), and a *long pointer* to the `BOOL` type (`*LPBOOL`). If you know C++, you already know about pointers. You don't ever need to know about long pointers; they became irrelevant when Windows 95 was introduced back in 1995. They're an old DOS/Win16 thing that mattered when microprocessor architecture was different. Nothing more than a curiosity of history now. – Cody Gray - on strike Apr 14 '11 at 13:30
15

FALSE is not defined in the standard. Only false is. true and false are called "Boolean literals", and are keywords in C++.

FALSE is sometimes defined as a macro. You can't rely on it being present on a standards-compliant environment as it is not part of the standard.

In other words, the following program is valid C++:

#include <iostream>

#define FALSE 1

int main()
{
    bool a = false;
    bool b = FALSE;
    std::cout << a << " " << b << std::endl;
    return 0;
}

and it will print 0 1 as expected. FALSE is not a privileged symbol in any way.

Andreas Haferburg
  • 5,189
  • 3
  • 37
  • 63
Mat
  • 202,337
  • 40
  • 393
  • 406
  • but FALSE and false return 0.. that means both type is bool.. right – RidaSana Apr 14 '11 at 13:07
  • 2
    not in standard C++. `TRUE` and `FALSE` can be defined (usually as a macro, sometimes as an `enum` value), but you can't rely on it. With `GCC` on Linux for example, `bool something = FALSE;` is a compile-time error because `FALSE` is not defined. When `FALSE` is defined, it will usually be defined as `0` or `false` - but that's not something you should rely upon. Rely upon the standards. – Mat Apr 14 '11 at 13:09
  • what about second part of the question about bool and BOOL? – RidaSana Apr 14 '11 at 13:21
  • same thing as @Code Gray pointed out. It's not a standard C++ type either. – Mat Apr 14 '11 at 13:22
  • @RidaSana: BOOL is often defined as "`typedef int BOOL;"` or "`typedef unsigned char BOOL;`". Again, this is a carry over from C when there was no type to represent Boolean values. – Thomas Matthews Oct 20 '12 at 16:50
  • 1
    and you can also do `#define false true` WOW! – Thomas Eding Dec 08 '12 at 19:21
2

in C++, false is a built-in literal of type boolean representing the false value (the other possible value is represented with the literal true)

FALSE is a #define whose definition may depends on the platform, but generally it is 0.

Gabriel Cuvillier
  • 3,617
  • 1
  • 28
  • 35
  • that mean false and FALSE are both boolean type... FALSE mean 0 but false should also be 0 because its a boolean type.. is't – RidaSana Apr 14 '11 at 13:05
  • 1
    false is not evaluated to 0, but coerced to 0 (or converted if you prefer, like when an int is implicitly converted to a float ). FALSE is rewritten to 0 by the preprocessor. Note that FALSE may also be defined by false, it depends on the platform. – Gabriel Cuvillier Apr 14 '11 at 13:19
  • 1
    @Miss: no,`FALSE` might be a Boolean type, but usually it is an integral type. You might use it as a Boolean value, but it is still integral. This is very important when it comes to template deduction and overloaded function resolution, for instance. `f(false)` and `f(FALSE)` can call two completely different functions if `FALSE` expands to `0`. – Dennis Zickefoose Apr 14 '11 at 13:19
1

false is a C++ keyword.

FALSE is not defined by the C++ standard. But most compilers have FALSE defined as a preprocessor macro with the value 0.

harper
  • 13,345
  • 8
  • 56
  • 105
  • @Miss: 'Predefined'? In most cases you will have to #include some header file to get the #define. – harper Apr 14 '11 at 18:31
1

Just remember what true and false Booleans always mean. They are always going to be integers (0,1). A compiler may not recognize it but they will always be stored and run through the CPU as integers. A debugger will show this. Whether it is in caps or not doesn't really matter because there should be no different in its implementation or usage. If you compiler has some macros written to support both upper and lower case true and false then great but it is just to fit your needs. Just like all the other types.

Hello
  • 11
  • 1
0

Important note on Windows API: some WIndows library functions return arbitrary integer values although the return type is defined as BOOL. It is best never to compare a BOOL to TRUE. use

  bSomeVal != FALSE instead.
Coding Mash
  • 3,338
  • 5
  • 24
  • 45