且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

什么是"!"用C?

更新时间:2022-10-19 11:41:00

是否定。因此, !! 是否定之否定。最重要的是一个事实,即结果将是一个 INT


  • !X 如果 X == 0 0!,即!1 ,即 0

  • !X 如果 X!= 0 !!(!0) ,即 !! 1 ,即!0 ,即 1

!! 是常用的,如果你想任何非零值转换为1而被肯定,0仍是0。

事实上, !! NULL == NULL ,因为!== NULL 0! 0!= =!1 最后!1 == 0

因此​​,在一小段code你提到的数组下标将是要么 0 如果括号中的前pression的值 NULL 1 其他。

I have encountered the following snippet:

pt->aa[!!(ts->flags & MASK)] = -val;

  1. What does !! stand for in c ?
  2. Isn't (!!NULL) == NULL ?

! is negation. So !! is negation of negation. What is important is the fact that the result will be an int.

  • !!x if x == 0 is !!0, that is !1, that is 0.
  • !!x if x != 0 is !!(!0), that is !!1, that is !0, that is 1.

!! is used commonly if you want to convert any non-zero value to 1 while being certain that 0 remains a 0.

And indeed, !!NULL == NULL, since !!NULL == !!0 and !!0 == !1 and finally !1 == 0.

Consequently, in the short piece of code you cited the array subscript will be either 0 if the value of the expression in parenthesis is NULL, and 1 otherwise.