惯用的方法来检查非零

惯用的方法来检查非零

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

问题描述

当我希望检查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.

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

08-20 11:52