Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between false and FALSE?

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.

like image 686
RidaSana Avatar asked Apr 14 '11 12:04

RidaSana


4 Answers

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.

like image 176
Cody Gray Avatar answered Nov 15 '22 09:11

Cody Gray


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.

like image 44
Mat Avatar answered Nov 15 '22 10:11

Mat


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.

like image 2
Gabriel Cuvillier Avatar answered Nov 15 '22 11:11

Gabriel Cuvillier


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.

like image 1
harper Avatar answered Nov 15 '22 11:11

harper