惯用的方法来检查非零 [英] Idiomatic way to check for non-zero

查看:86
本文介绍了惯用的方法来检查非零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我希望检查C中的值是否为0时,该怎么习惯?

When I wish to check if a value is 0 in C, how is it idiomatically done?

  • if (!num)
  • if (num == 0)
  • if (!num)
  • if (num == 0)

推荐答案

虽然这是一个品味问题,但我发现它很大程度上取决于意图.如果将该值用作布尔值,则!没问题.如果值在计算某些事情,那么平等就更有意义了.

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.

这篇关于惯用的方法来检查非零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆