简体   繁体   English

NOT false和true之间的区别

[英]Difference between NOT false and true

I have this chunk of code: 我有这段代码:

if ( ( data.is_err != FALSE ) && ( available != FALSE ) )
    {
        Set_Dtc_TimerChange(DTC_VPOS, +dt_ms);
    }

Why would you use not equal to false instead of just equal to true? 为什么你会使用不等于假而不是等于真? Is there a difference at all? 有什么不同吗?

This is a C question rather than an EE, but as C is very close to the hardware.... 这是一个C问题,而不是EE,但由于C非常接近硬件....

The old C definition of false is 0, with all other values being true , that is 旧的C定义false为0, 其他所有值都为true ,即

if( 0 ){ ... }

will not execute the ... code, but for instance 不会执行...代码,但是例如

if( 1 ){ ... }
if( -1 ){ ... }
if( 42 ){ ... }

all will. 一切都会。 Hence you can test a truth value for being equal to FALSE (which is defined as 0), but when you want to compare it to true, which value would you use for true? 因此,您可以测试真值等于FALSE(定义为0),但是当您想要将其与true进行比较时,您将使用哪个值为true? Some say TRUE must be defined as (!FALSE), but that would yield -1, which is a value of true, but not the only one. 有人说TRUE必须定义为(!FALSE),但这会产生-1,这是一个值true,但不是唯一值。

Hence programmers tended to avoid conparing to TRUE. 因此,程序员倾向于避免使用TRUE。

Modern C should have solved this by defining TRUE to be (I think) 1, but that stil has funny consequences, like 现代C应该通过将TRUE定义为(我认为)1来解决这个问题,但是stil会产生有趣的后果,比如说

int probably( void ){ return 42; }
if( probably() ){ ... }
if( probably() == TRUE ){ ... }

Here the blame is on probbaly(), which returns an abiguous truth value. 这里的责任在于probbaly(),它返回一个明确的真值。

Personally, I would have written 就个人而言,我会写的

if ( data.is_err && available ) { ... }

which I think is much more readable and avoids the comparisons. 我认为它更具可读性,避免了比较。

(BTW how can something be available when there is a data error?) (顺便说一句,当出现数据错误时,有什么东西可用?)

This code can be safely replaced by: 此代码可以安全地替换为:

if (  data.is_err &&  available ) { 
    Set_Dtc_TimerChange(DTC_VPOS, +dt_ms); 
}

unless the FALSE has some non-traditional definition somewhere in the program. 除非FALSE在程序的某处有一些非传统的定义。 A good compiler will produce equivalent code for either form, but the more readable one is preferred. 一个好的编译器将为任一形式生成等效代码,但更可读的更好。

C does not have a Boolean type. C没有布尔类型。 Instead, it treats 0 as false and any other value as true. 相反,它将0视为false,将任何其他值视为true。 As a result, there is no value that you can test against to determine whether a value represents true. 因此,没有值可以测试以确定值是否为true。 That's not important if you write idiomatic C code: if (x) ... will always do the right thing. 如果您编写惯用的C代码,这并不重要: if (x) ...将始终做正确的事情。 Folks who are uncomfortable with this idiom like to test explicitly against some representation of false, but never explain why they limit that to one level. 对这个成语感到不舒服的人喜欢明确地对某些虚假的表示进行测试,但从不解释为什么他们将这限制在一个级别。 After all, x != FALSE is just a value, and by the same argument needs to be tested: if ((x != FALSE) != FALSE)... . 毕竟, x != FALSE只是一个值,并且需要测试相同的参数: if ((x != FALSE) != FALSE)...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM