It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center
7年前关闭。
下面的陈述到底在做什么?
我很困惑我很惊讶C允许您在if语句中使用条件运算符。有更好的办法吗?
我发现if语句中的条件运算符太混乱了。我可以尝试使用&&和|运算符,但恐怕我可能会犯错误。
如何将下面的语句重写为更简单的形式?
if ( (offset < 0) ?
         ( input->binData.bounds.lo >= (unsigned long)(-offset) ) :
         ( input->binData.bounds.hi < (unsigned long)(-offset) ) )

我在Bithuft开源包中找到了这行C代码。

最佳答案

您可以使用三元运算符的每个分辨率来AND三元条件(offset < 0)及其相反的条件(offset >= 0):

if ( ((offset < 0) && ( input->binData.bounds.lo >= (unsigned long)(-offset) )) ||
     ((offset >= 0) && ( input->binData.bounds.hi <  (unsigned long)(-offset) )) ) {

关于c - 令人困惑的“if”陈述,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11605533/

10-11 15:31