C中的逻辑相等 [英] Logical equality in C

查看:36
本文介绍了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 中,我认为

In C99, I think that

(bool)a == (bool)b

给我想要的.那是对的吗?用传统 C 写这个的正常方式是什么?

gives what I want. Is that correct? What is the normal way of writing this in traditional C?

推荐答案

你通常会看到:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆