开发者

What is the difference between false and FALSE?

开发者 https://www.devze.com 2023-02-25 08:55 出处:网络
In many programs, I see statements with the identifiers FALSE and false. Is there any difference between them in the context of C++?

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 anyon开发者_StackOverflow中文版e explain the difference between these identifiers to me? Thanks.


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.


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.


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.


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.


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.


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.
0

精彩评论

暂无评论...
验证码 换一张
取 消