Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)
like image 715
Paul Manta Avatar asked Oct 26 '11 09:10

Paul Manta


1 Answers

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.
like image 88
DarkDust Avatar answered Nov 12 '22 13:11

DarkDust