是的,有两个。
如果a
为非零,则!!a
为1
;如果a
为0
,则0
为0
您可以将!!
视为对{0,1}
的钳制。我个人认为这种用法是一种花哨的尝试。
你可以这样想象:
!(!(a))
如果你一步一步做,这是有道理的
result = !42; //Result = 0
result = !(!42) //Result = 1 because !0 = 1
这将返回1
与任何数字(-42,4.2 f 等),但只有0
,这将发生
result = !0; //Result = 1
result = !(!0) //result = 0
!!
是(_Bool)
的更便携(C99 之前)的替代方法。
你说得对。这是两个 nots。要了解为什么会这样做,请尝试以下代码:
#include <stdio.h>
int foo(const int a)
{
return !!a;
}
int main()
{
const int b = foo(7);
printf(
"The boolean value is %d, "
"where 1 means true and 0 means false.\n",
b
);
return 0;
}
它输出The boolean value is 1, where 1 means true and 0 means false.
如果删除!!
,它输出The boolean value is 7, where 1 means true and 0 means false.
本站系公益性非盈利分享网址,本文来自用户投稿,不代表码文网立场,如若转载,请注明出处
评论列表(23条)