This program anecdotally shows that the integer value of ! 0
is 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
$
Is this guaranteed by any standard, or could ! 0
ever evaluate to any other nonzero integer value?
It is guaranteed to evaluate to 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)
.