什么是!0用C? [英] What is !0 in C?

查看:147
本文介绍了什么是!0用C?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道,在C,if语句和比较FALSE = 0和其他任何等于true。

因此​​,

  INT J = 40
INT K =!Ĵ满足K == 0 //这是真的

我的问题处理相反。这是什么!0变成什么? 1?

  int类型l = 0
INT M =镜!米==? // M是什么?


解决方案

布尔/用C逻辑运算都需要产生0或1。

从ISO C99标准的部分6.5.3.3/5:


  

逻辑否定运算符的结果 0如果操作数的值进行比较,不等于0,1,如果它的操作数的值进行比较,等于0


其实,!X 是强制的值是一个常见的​​成语是0或1(我个人preFER X != 0 ,虽然)。

也看到从comp.lang.c常见问题解答 Q9.2。

I know that in C, for if statements and comparisons FALSE = 0 and anything else equals true.

Hence,

int j = 40
int k = !j

k == 0 // this is true

My question handles the opposite. What does !0 become? 1?

int l = 0
int m = !l

m == ? // what is m?

解决方案

Boolean/logical operators in C are required to yield either 0 or 1.

From section 6.5.3.3/5 of the ISO C99 standard:

The result of the logical negation operator ! is 0 if the value of its operand compares unequal to 0, 1 if the value of its operand compares equal to 0.

In fact, !!x is a common idiom for forcing a value to be either 0 or 1 (I personally prefer x != 0, though).

Also see Q9.2 from the comp.lang.c FAQ.

这篇关于什么是!0用C?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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