C语言 在 C 中使用 true 和 false

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2254075/
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-09-02 04:28:38  来源:igfitidea点击:

Using true and false in C

ccoding-style

提问by Tom

As far as I can see there are 3 ways to use booleans in c

据我所知,在 c 中有 3 种使用布尔值的方法

  1. with the bool type, from then using true and false
  2. defining using preprocessor #define FALSE 0 ... #define TRUE !(FALSE)
  3. Just to use constants directly, i.e. 1 and 0
  1. 使用 bool 类型,然后使用 true 和 false
  2. 使用预处理器定义 #define FALSE 0 ... #define TRUE !(FALSE)
  3. 只是直接使用常量,即 1 和 0

are there other methods I missed? What are the pros and cons of the different methods?

我错过了其他方法吗?不同方法的优缺点是什么?

I suppose the fastest would be number 3, 2 is more easily readable still (although bitwise negation will slightly add to overhead), 1 is most readable not compatible with all compilers.

我想最快的是数字 3,2 仍然更容易阅读(尽管按位否定会稍微增加开销),1 是最易读的,与所有编译器不兼容。

回答by Chris Jester-Young

Just include <stdbool.h>if your system provides it. That defines a number of macros, including bool, false, and true(defined to _Bool, 0, and 1 respectively). See section 7.16 of C99 for more details.

<stdbool.h>如果您的系统提供它,只需包括它。它定义了许多宏,包括boolfalsetrue(分别定义为_Bool、 0 和 1 )。有关更多详细信息,请参阅 C99 的第 7.16 节。

回答by b.roth

Just use 0 or 1 directly in the code.

只需在代码中直接使用 0 或 1。

For C programmers, this is as intuitive as true or false.

对于 C 程序员来说,这就像真假一样直观。

回答by Macarse

I usually do a:

我通常做一个:

typedef enum {FALSE = 0, TRUE} boolean;

回答by Doug T.

With the stdbool.h defined bool type, problems arise when you need to move code from a newer compiler that supports the bool type to an older compiler. This could happen in an embedded programming environment when you move to a new architecture with a C compiler based on an older version of the spec.

使用 stdbool.h 定义的 bool 类型,当您需要将代码从支持 bool 类型的较新编译器移动到较旧编译器时会出现问题。当您使用基于旧版本规范的 C 编译器迁移到新架构时,这可能会发生在嵌入式编程环境中。

In summation, I would stick with the macros when portability matters. Otherwise, do what others recommend and use the bulit in type.

总而言之,当便携性很重要时,我会坚持使用宏。否则,按照其他人的建议并使用内置类型。

回答by Bob Wakefield

Whichever of the three you go with, compare your variables against FALSE, or false.

无论您选择这三个中的哪一个,将您的变量与 FALSE 或 false 进行比较。

Historically it is a bad idea to compare anythingto true (1) in c or c++. Only false is guaranteed to be zero (0). True is any other value. ? Many compiler vendors have these definitions somewhere in their headers. ?

从历史上看,在 c 或 c++ 中将任何内容与 true (1)进行比较是一个坏主意。只有 false 保证为零 (0)。True 是任何其他值。? 许多编译器供应商在其头文件中的某处都有这些定义。?

#define TRUE 1
#define FALSE 0

This has led too many people down the garden path. ? Many library functions besides chartypereturn nonzero values not equal to 1on success. There is a great deal of legacy code out there with the same behavior.

这导致太多人走上花园小径。? 除了成功之外,许多库函数还chartype返回不等于的非零值1。有大量具有相同行为的遗留代码。

回答by Nick Van Brunt

You can test if bool is defined in c99 stdbool.h with

您可以测试是否在 c99 stdbool.h 中定义了 bool

#ifndef __bool_true_false_are_defined || __bool_true_false_are_defined == 0
//typedef or define here
#endif

回答by anthares

I would go for 1. I haven't met incompatibility with it and is more natural. But, I think that it is a part of C++ not C standard. I think that with dirty hacking with defines or your third option - won't gain any performance, but only pain maintaining the code.

我会选择 1。我没有遇到与它不兼容的情况,并且更自然。但是,我认为它是 C++ 的一部分,而不是 C 标准。我认为使用定义或您的第三个选项进行肮脏的黑客攻击 - 不会获得任何性能,而只会使维护代码变得痛苦。

回答by wallyk

Any int other than zero is true; false is zero. That way code like this continues to work as expected:

任何非零的 int 都是真的;假为零。这样,像这样的代码会继续按预期工作:

int done = 0;   // `int` could be `bool` just as well

while (!done)
{
     // ...
     done = OS_SUCCESS_CODE == some_system_call ();
}

IMO, boolis an overrated type, perhaps a carry over from other languages. intworks just fine as a boolean type.

IMO,bool是一种被高估的类型,可能是其他语言的结转。 int作为布尔类型工作得很好。

回答by Lothar

I prefer (1) when i define a variable but in expressions i never compare against true and false just take the implicit C definition of if(flag) or if(!flag) or if(ptr). Thats the C way to do things.

我更喜欢 (1) 当我定义一个变量但在表达式中我从不与 true 和 false 比较时只采用 if(flag) 或 if(!flag) 或 if(ptr) 的隐式 C 定义。这就是做事的C方式。

回答by Marco Demaio

I used to use the #define because they make code easier to read, and there should be no performances degradation compared to using numbers (0,1) coz' the preprocessor converts the #define into numbers before compilation. Once the application is run preprocessor does not come into the way again because the code is already compiled.

我曾经使用 #define 是因为它们使代码更易于阅读,并且与使用数字 (0,1) 相比,性能应该不会下降,因为预处理器会在编译前将 #define 转换为数字。一旦应用程序运行,预处理器就不会再出现,因为代码已经编译好了。

BTW it should be:

顺便说一句,它应该是:

#define FALSE 0 
#define TRUE 1

and remember that -1, -2, ... 2, 3, etc. all evaluates to true.

并记住 -1、-2、... 2、3 等都评估为真。