int main() {
int x = -1;
if (!x) {
printf("Yes\n");
}
}
Is !x
true when x
is a negative number or it's true when it's only 0
?
For any non-zero x
, !x
will be zero. So, for x == -1
, !x
is false.
From cppreference:
The logical NOT operator has type int. Its value is 0 if expression evaluates to a value that compares unequal to zero. Its value is 1 if expression evaluates to a value that compares equal to zero.
Any non-zero value is true even if it is negative value(e.g. -1 is true). So, negation of true is false. In the case of x=-1, if (x) will be evaluated to true. Thus, if(!x) will be false, and the printf("Yes\n"); will never be executed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With