What is “!!” in C? [duplicate]

Deadly 提交于 2019-12-17 07:20:17

问题


I have encountered the following snippet:

pt->aa[!!(ts->flags & MASK)] = -val;
  1. What does !! (double exclamation marks / exclamation points/ two not operators) stand for in c?
  2. Isn't (!!NULL) == NULL ?

回答1:


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




回答2:


It is commonly (ab)used to convert any value into the ints 0 or 1 by repeated application of the boolean not operator, !.

For instance: !56 is 0, since 56 is "true" when viewed as a boolean. This means that !!56 is 1, since !0 is 1.




回答3:


!E is the same as E == 0 so !!E is the same as (E == 0) == 0. !! is used to normalize booleans values.




回答4:


In C99 you can replace it by

#include <stdbool.h>


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

Of course if your code is to be portable to C89 then you'd be better off doing the !! trick or

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

or

pt->aa[(ts->flags & MASK)?1:0] = -val;

The generated code will be certainly identical.




回答5:


It converts a number into a canonical Boolean.

And note that in this case it's critical to do so, since the result is being used to index an array.




回答6:


  1. !!x is just a !(!x).
  2. if NULL is defined as 0 then !!NULL == !!0 == !(!0) == !(1) == 0.



回答7:


!! is a decent way to quiet the compiler in certain situations such as assignment in a conditional with more than one expressions, e.g:

int _blah = 100;
int *blah;
if ( _blah > 100 && !!(blah = &_blah) ) {
// do stuff
}

I don't recommend this -- warnings are usually there to enforce good coding practice.



来源:https://stackoverflow.com/questions/14751973/what-is-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!