更新时间:2022-10-28 13:56:43
布尔/用C逻辑运算都需要产生0或1。
从ISO C99标准的部分6.5.3.3/5:
逻辑否定运算符的结果
!
0如果操作数的值进行比较,不等于0,1,如果它的操作数的值进行比较,等于0
块引用>其实,
!X
是强制的值是一个常见的成语是0或1(我个人preFERX != 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 preferx != 0
, though).Also see Q9.2 from the comp.lang.c FAQ.