Compound condition in C: if (0.0 < a < 1.0)

后端 未结 4 2061
忘掉有多难
忘掉有多难 2020-12-04 02:31

I recently noticed that the following expression compiles in my compiler (Clang):

float a;
if (0.0 < a < 1.0) { ... }

Does this do wh

4条回答
  •  鱼传尺愫
    2020-12-04 03:11

    Does this do what I expect?

    No, it does

    (0.0 < a) < 1.0
    

    and (0.0 < a) yields 0 or 1. So you end up having 0 < 1.0 or 1 < 1.0 evaluated.

    Note that in some other languages it does what you expect.

    For example in Python,

    a < b < c
    

    is called a chained comparison and it is equivalent to:

    (a < b) and (b < c)
    

提交回复
热议问题