Logical equality in C

前端 未结 4 604
暗喜
暗喜 2021-01-17 20:07

[It seems odd this doesn\'t exist, so apologies in advance if it\'s a duplicate]

I want to test for logical equality in C. In other words, I want to know whether tw

相关标签:
4条回答
  • 2021-01-17 20:37

    There is no (bool) in traditional c. True/False is handled using ints. You can check for boolean equality with

    a ? b : !b
    
    0 讨论(0)
  • 2021-01-17 20:39

    You typically see this:

    if ((a == 0) == (b == 0))
    

    Or

    if (!!a == !!b)
    

    Since !!a evaluates to 1 if a is nonzero and 0 otherwise.

    Hope this helps!

    0 讨论(0)
  • 2021-01-17 20:43

    In C, zero is false. If you want to convert any value to its boolean equivalent, the standard way (well, except that there's almost never a need for it) is to prefix an expression with !! , as in !!a. In the case of your expression, !!a == !!b may be simplified to !a == !b

    0 讨论(0)
  • 2021-01-17 20:50

    In pre-C99 C, the tradiitional, idiomatic way to "cast to bool" is with !!.

    0 讨论(0)
提交回复
热议问题