This is not the way to test an interval in c. You should use if (-600 <= a && a <= 600)
instead.
The test (-600 <= a <= 600)
does not have the same meaning in C as its corresponding mathematical statement, instead it is interpreted by the compiler as (-600 <=a) <= 600
will first evaluate -600 <= a
to 0
or 1
which is then interpreted as integer and compared to be smaller than 600 in the second test.
Compile the code with all warnings enabled -Wall
is a good way of avoiding common mistakes. For this code clang and gcc gives different warning messages but the meaning is the same.
Clang:
so13.c:9:18: warning: result of comparison of constant 600 with boolean expression is always true [-Wtautological-constant-out-of-range-compare]
if(-600 <= a <= 600)
~~~~~~~~~ ^ ~~~
gcc:
so13.c: In function ‘main’:
so13.c:9:18: warning: comparison of constant ‘600’ with boolean expression is always true [-Wbool-compare]
if(-600 <= a <= 600)
^~
so13.c:9:13: warning: comparisons like ‘X<=Y<=Z’ do not have their mathematical meaning [-Wparentheses]
if(-600 <= a <= 600)
~~~~~^~~~