What does “Comparing constant with boolean expression is always true” warning mean?

前端 未结 3 2019
死守一世寂寞
死守一世寂寞 2020-12-20 01:15

What does this warning mean (i and j are not constants):

I have been trying to Google this but it does not give me any results.

3条回答
  •  悲哀的现实
    2020-12-20 01:39

    In C, chaining of relational operators like this are not valid design. Thus,

     (0<=i<=10)
    

    is not doing what you think it should be doing. it is getting evaluated as

    ((0<=i) <= 10 )
    

    which is basically either

    • 0 < = 10, producing 1 (considered TRUE value)
    • 1 < = 10, also producing 1 (considered TRUE value)

    sadly, both of which are way out than the expected path.

    Solution: you need to break down your condtion check like

     (0 <= i) && ( i<=10)
    

提交回复
热议问题