'Idiomatic way to check for non-zero
When I wish to check if a value is 0
in C, how is it idiomatically done?
if (!num)
if (num == 0)
Solution 1:[1]
While this is a matter of taste, I find it pretty much depends on intention. If the value is to be used as a boolean, !
is alright. If the value is counting something the equality makes more sense.
if (!isVisible) {...}
if (isVisible == 0) {...} // Intention not as clear as line above.
if (numberOfItems == 0) {...}
if (!numberOfItems) {...} // Intention not as clear as line above.
Solution 2:[2]
I always prefer the second way:
if (num == 0)
As num == 0
or ptr == NULL
evaluates to a boolean which is the intent. The Java compiler enforces this form, but C/C++ compilers don't.
The worst example of this would be:
if (!strcmp(str, "something"))
Which really disguises its intent as the strcmp
family of functions don't return boolean, they return positive, zero, or negative (as pointed out by @JoachimPileborg).
However if the int
is being used to represent a boolean type, which C does not have a builtin type for, then this form is OK:
if (!b)
But this can be made self documenting by creating a custom type:
typedef int bool;
#define true 1
#define false 0
bool b = true;
if (!b)
{
... etc
}
Solution 3:[3]
Whatever the others told you WITH AN EXCEPTION!
Don't do it with float
and double
. IEEE 754 float
s/double
s/long double
s (the most commonly used) often don't contain exact values, so comparing them directly with 0
is foolish (or doing if (!floatValue)
)
Example: http://ideone.com/PIUflA
float f = 0.3;
f -= 0.2;
f -= 0.1;
if (!f)
{
printf("zero\n");
}
else
{
printf("non zero\n");
}
if (f == 0)
{
printf("zero\n");
}
else
{
printf("non zero\n");
}
With unoptimized compilation can return (on ideone does)
non zero
non zero
(if you enable optimizations, the compiler could pre-compute some values in higher precision and round them to 0
)
Solution 4:[4]
I think it depends on the context. If the variable refers to a boolean value is better first choice. Otherwise, the second is better.
Solution 5:[5]
It's done however you want it to be done, in terms of your style. I don't see it as mattering as long as your consistent and it's clear on what you're trying to do, or if you do it in cases where it may flow better in an English sentence and may put emphasis on what your doing.
For sake of clarity I usually have if (num == 0)
, since it takes less thinking to understand what I'm doing when I'm going over my code.
Solution 6:[6]
!!value
will work too if you want to check if value is non-zero.
!value
evaluates to 1 when value=0 and 0 when value?0.
The second !
flips it, making !!value
evaluate to 1 when value?0 and 0 when value=0.
Solution 7:[7]
We may argue which way is better, but idiomatic, though for what I can tell by other answers archaic, would be if(!num)
.
Solution 8:[8]
For the compiler, it does not matter, of course. For the human reader it does. Since both forms are used by other human beings, you should get used to them and recognise them. Personally, I prefer the shortest form, which takes less time (less tokens, especially parenthesis) to read and understand. Especially:
if (!ptr) {}
if (!strcmp(a,b)) {}
are easyer to read than
if (ptr != NULL) {}
if (strcmp(a,b) == 0) {}
if (0 == strcmp()) {}
The last form makes me physically sick.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | unwind |
Solution 3 | |
Solution 4 | vicentazo |
Solution 5 | AusCBloke |
Solution 6 | Alexey Frunze |
Solution 7 | Michael Krelin - hacker |
Solution 8 |