0

How the condition checking execute in "if" condition?

int main()
{
    int a = 0;
    printf("Enter the number\n");
    scanf("%d",&a);
    if(-600 <= a <= 600)
    {
        printf("True");
    }
    else
    {
        printf("False");
    }
    return 0;
}

The output is "True". Kindly clarify what is happening in that "if" condition?

Simson
  • 3,373
  • 2
  • 24
  • 38
  • 2
    That syntax is invalid and unsupported. I guess it ends up as if with parenthesis, (-600 <= a) <= 600 which is true/false <= 600 or 0/1 <= 600 which is always true. same with the () on the right side – B. Go Mar 15 '20 at 18:06
  • 1
    As the B.GO Answered, proper condition in programming languages would be: `-600 <= a && a <= 600`, you need to divide each condition and join them with && (and) or || (or). Line what is written in short by `-600 <= a <= 600` when you read normally would be something like: `a is <= than 600 but(end) >= than -600` and so on – Seti Mar 15 '20 at 18:40
  • 5
    @B.Go: That syntax is neither invalid nor unsupported. `a <= b <= c` is valid per the grammar in C 2018 6.5.8 1. It is merely not what the OP desires. – Eric Postpischil Mar 15 '20 at 20:23
  • duplicates: [Is (4 > y > 1) a valid statement](https://stackoverflow.com/q/8889522/995714), [Language support for chained comparison operators (x < y < z)](https://stackoverflow.com/q/4090845/995714), [Compound condition in C: if (0.0 < a < 1.0)](https://stackoverflow.com/q/17878632/995714), [Why can't I just use 51 <= j <= 55? / data types](https://stackoverflow.com/q/54168808/995714), [Is (val1 > val2 > val3) a valid comparison in C?](https://stackoverflow.com/q/38643022/995714) – phuclv Mar 16 '20 at 02:14
  • Does this answer your question? [Compound relational operators in C](https://stackoverflow.com/questions/17031880/compound-relational-operators-in-c) – B. Go Mar 16 '20 at 13:27

1 Answers1

0

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)
        ~~~~~^~~~
Simson
  • 3,373
  • 2
  • 24
  • 38