double negation in C : is it guaranteed to return 0/1?

前端 未结 2 501
Happy的楠姐
Happy的楠姐 2020-12-03 20:51

Is !!(x) guaranteed by the standard to return 0/1?

Note that I am not asking about c++, where a bool type is defined.

2条回答
  •  时光说笑
    2020-12-03 21:16

    Yes, in C99, see §6.5.3.3/4:

    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. The result has type int. The expression !E is equivalent to (0==E).

    So !x and !!y can only yield 0 or 1, as ints.

    For other operators, in C99, see also Is the "true" result of >, <, !, &&, || or == defined?

提交回复
热议问题