What double negation does in C

后端 未结 5 599
谎友^
谎友^ 2021-01-14 05:34

I had a controversy about what compilers \"think\" about this:

a = 8;
b = !!a;

So, is b == 0x01 ? Is TRUE always

5条回答
  •  终归单人心
    2021-01-14 06:01

    You have not supplied enough information to tell whether !!a works for your purposes or not.

    You stated that you really need the result of a > 0. However, this is not equivalent to !!a if a is signed and holds a negative value. If this is possible, then the answer is obviously "no".

    !!a is equivalent to a != 0, not to a > 0.

提交回复
热议问题