C++ false 和 FALSE 和有什么不一样?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5663601/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 18:38:02  来源:igfitidea点击:

What is the difference between false and FALSE?

c++windowsboolean

提问by RidaSana

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

在许多程序中,我看到带有标识符FALSEfalse. 在 C++ 的上下文中,它们之间有什么区别吗?

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

同样在某些程序中,我在bool某处看到了BOOL. 这两者之间有什么区别?

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

谁能向我解释这些标识符之间的区别?谢谢。

回答by Cody Gray

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

如果您已包含windows.h,则FALSE#defined 为 0。# TRUEdefined 为 1。它不是布尔类型,而是整数类型。Windows 还定义了类型BOOL,它实际上是一个整数类型,旨在容纳值TRUEFALSE

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

中的相关代码部分windows.h如下所示:

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

Windows 最初是用 C 编写的,所有原始的 Windows 应用程序也是如此。当时,C 没有标准定义的布尔类型。相反,开发人员被迫定义自己的布尔常量。为了防止可能造成的混乱,Microsoft 决定制定一个适用于所有 Windows 应用程序的标准。

If you're writing Windows applications, you may still need to use the TRUEand FALSEidentifiers. Several of the functions defined in windows.heither accept Boolean parameters (actually integers!) typed as BOOLor 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 trueand falsekeywords now defined by the standard in all of the methods that I write myself.

如果您正在编写 Windows 应用程序,您可能仍需要使用TRUEFALSE标识符。中定义的几个函数windows.h要么接受类型化的布尔参数(实际上是整数!),BOOL要么返回这种类型的值。C++ 是强类型的,如果您将它们强制转换为语言标准中定义的布尔类型,则可能会抱怨。为保持一致性,我在对 Windows API 函数的所有调用中使用旧标识符,并且在我自己编写的所有方法中现在由标准定义的trueandfalse关键字。

There's really no reason not to use FALSEas 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.

真的没有理由不使用,FALSE因为它在windows.h. 但是你不应该像其他人提到的那样在专有头文件中自己定义它,因为不能保证定义会随着时间的推移保持不变。这可能会导致一些非常愚蠢的错误,这些错误有资格出现在 Daily WTF 上。

Mat's answeris 100% correct that neither TRUEor FALSEare defined by the C or C++ standards. However, they arestrictly 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.

垫的回答是100%正确的,无论TRUEFALSE由C或C ++标准中定义。然而,他们严格由Windows API定义,并且保证不会改变。使用它们不会产生真正的跨平台、严格符合标准的 C++ 代码,但在编写 Windows 应用程序时很少考虑这一点,因为在windows.h.

回答by Mat

FALSEis not defined in the standard. Only falseis. trueand falseare called "Boolean literals", and are keywords in C++.

FALSE标准中没有定义。只有false是。truefalse被称为“布尔文字”,是 C++ 中的关键字。

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

FALSE有时定义为宏。您不能依赖它出现在符合标准的环境中,因为它不是标准的一部分。

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

换句话说,以下程序是有效的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 1as expected. FALSEis not a privileged symbol in any way.

它会0 1按预期打印。FALSE无论如何都不是特权象征。

回答by Gabriel Cuvillier

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

在 C++ 中,false是表示 false 值的 boolean 类型的内置文字(另一个可能的值用文字true表示)

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

FALSE 是一个#define,其定义可能取决于平台,但一般为0。

回答by harper

falseis a C++ keyword.

false是一个 C++ 关键字。

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

FALSE 不是由 C++ 标准定义的。但是大多数编译器都将 FALSE 定义为值为 0 的预处理器宏。

回答by Hello

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.

只要记住 true 和 false 布尔值总是意味着什么。它们总是整数 (0,1)。编译器可能无法识别它,但它们将始终作为整数存储并通过 CPU 运行。调试器将显示这一点。是否大写并不重要,因为它的实现或用法应该没有什么不同。如果您的编译器编写了一些宏来支持大小写 true 和 false,那么很好,但它只是为了满足您的需求。就像所有其他类型一样。

回答by Dr DeeJay

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 BOOLto TRUE. use

Windows API 的重要说明:一些 WINdows 库函数返回任意整数值,尽管返回类型定义为BOOL. 最好永远不要将 aBOOL与进行比较TRUE。用

  bSomeVal != FALSE instead.