cif-statementcomparisonoperators

Multiple Comparison operators?


im having trouble using multiple operators while programing a simple FizzBuz in C

#include <stdio.h>
int main(void) {
    int n;
    scanf("%d", &n);
    if (0 < n < 10000){
        for (int i = 1; i <= n; i ++) {
            if (i % 15 == 0) {
                puts("TikTak");
            }
            else if (i % 3 == 0) {
                puts("Tik");
            }
            else if (i % 5 == 0) {
                puts("Tak");
            }
            else {
                printf("%d\n", i);
            }
        }
    }else{
        printf("-1");
    }
}

Now the "if (0 < n < 10000)" comparison operators is ignored for some reason, but if I rewrite it as:

if (n < 10000 && n > 0){

it will work as intended.

Am I missing something? Ill be honest Im newbie in C programing. haha

EDIT: Thanks everybody, haha that was quite simple. I thought that might be the issue I just waned to make surebecause "0 < n < 10000" is litteraly how the assigment says it should look like.

Again, thanks!


Solution

  • Rewrite this condition

    if (0 < n < 10000){
    

    like

    if (0 < n && n < 10000){
    

    Otherwise the original condition looks like

    if (( 0 < n ) < 10000){
    

    and the result of the expression 0 < n is either 1 or 0. So in fact you are comparing 0 or 1 with 10000.

    From the C Standard (6.5.8 Relational operators)

    6 Each of the operators < (less than), > (greater than), <= (less than or equal to), and >= (greater than or equal to) shall yield 1 if the specified relation is true and 0 if it is false.107) The result has type int.