Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.4k views
in Technique[技术] by (71.8m points)

c - What does "Comparing constant with boolean expression is always true" warning mean?

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.

warning: comparison of constant 10 with boolean expression is always true [-Wtautological-constant-out-of-range-compare]

 if ((0<=i<=10)&&(0<=j<=10)){

In my program, i and j are not constant values and they do change.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...