A chain of several more than / less than operators

In the statement, ifI want to include a range, for example:

if(10 < a < 0)

but at the same time, I get a "meaningless comparison" warning. However, this works great without warning:

if(a<10 && a>0)

Is it possible to implement the first case in C?

+7
source share
6 answers

Please note that the original version if(10 < a < 0)is absolutely legal. He just does not do what you (reasonably) think he does. You are fortunate that the compiler acknowledged this as a possible mistake and warned you about it.

< , +. , a + b + c (a + b) + c, a < b < c (a < b) < c. < int 0, , 1, . , , 0, c, 1 , c.

, , , , , . , , , , , "" . ( , , (a < b) < c).)

, a b b c:

a < b && b < c

( , Python, a < b < c a<b && b<c, . C .)

+29

, , 2.

+6

, .
, .

+5

, . , (10<a) < 0. , , , < 0 1. , 0, false.

, , . 0, 10? , . , 0<a<10 a>0 && a<10.

, : if ((unsigned)a < 10) true, 0..10. , :

if ((unsigned)(x-range_start)<(range_end-range_start))
    // in range
else
    // out of range.

. , , (, , ).

+2

, . , . 10 < a = True False. True/False < 0, .

0

no, this is invalid syntax for the if statement, it must have a valid constant expression, or it can have logical operators in it and only execute when the expression in the bracket is true or a non-zero value

-2
source

All Articles