C语言中的逻辑相等 [英] Logical equality in C
本文介绍了C语言中的逻辑相等的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
[这似乎并不奇怪,所以如果重复的话,请提前道歉]
[It seems odd this doesn't exist, so apologies in advance if it's a duplicate]
我想测试C中的逻辑相等性.换句话说,我想知道如果两个值都以与逻辑表达式关联的正常方式转换,那么两个值是否相等.
I want to test for logical equality in C. In other words, I want to know whether two values would be equal if both were converted in the normal way associated with logical expressions.
在C99中,我认为
(bool)a == (bool)b
给出我想要的.那是对的吗?用传统的C语言编写此代码的正常方式是什么?
gives what I want. Is that correct? What is the normal way of writing this in traditional C?
推荐答案
您通常会看到以下内容:
You typically see this:
if ((a == 0) == (b == 0))
或
if (!!a == !!b)
由于!!a
如果a为非零值,则为1,否则为0.
Since !!a
evaluates to 1 if a is nonzero and 0 otherwise.
希望这会有所帮助!
这篇关于C语言中的逻辑相等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文