标准是否保证 !!(x)
返回 0/1?
请注意,我不是询问有关定义 bool 类型的 c++。
最佳答案
是的,在 C99 中,请参阅 §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).
所以 !x
和 !!y
只能产生 0 或 1,作为 int
。
对于其他运算符,在 C99 中,另请参阅 Is the "true" result of >, <, !, &&, || or == defined?
关于c - C : is it guaranteed to return 0/1? 中的双重否定,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8617090/