在 C 中,!0 总是 return 1 吗?
In C, will !0 always return 1?
这个程序有趣地表明 ! 0
的整数值为 1
:
#include <stdio.h>
int main( int argc, char* argv[] ) {
printf( "%d\n", ! 0 );
return 0;
}
$ gcc --version && gcc -g ./main.c && ./a.out
gcc (Debian 6.3.0-18+deb9u1) 6.3.0 20170516
Copyright (C) 2016 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
1
$
这是否有任何标准保证,或者 ! 0
是否可以评估任何其他非零整数值?
保证计算结果为 1
。
6.5.3.3 Unary arithmetic operators
...
5 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)
.
C 2011 Online Draft
这个程序有趣地表明 ! 0
的整数值为 1
:
#include <stdio.h>
int main( int argc, char* argv[] ) {
printf( "%d\n", ! 0 );
return 0;
}
$ gcc --version && gcc -g ./main.c && ./a.out
gcc (Debian 6.3.0-18+deb9u1) 6.3.0 20170516
Copyright (C) 2016 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
1
$
这是否有任何标准保证,或者 ! 0
是否可以评估任何其他非零整数值?
保证计算结果为 1
。
6.5.3.3 Unary arithmetic operatorsC 2011 Online Draft
...
5 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 typeint
. The expression!E
is equivalent to(0==E)
.