I don't know what's wrong with the C language if statement. ( if (0 < num1 < 45) )

Asked 2 years ago, Updated 2 years ago, 88 views

#include <stdio.h>

void main()
{

    int num1;

    scanf_s("%d", &num1);

    if (0 < num1 < 45)
        printf("a");

    else if (90 < num1 < 160)
        printf("b");

    else
        printf ("None";
}

What's wrong is only a and nothing else.

if문 elseif c

2022-09-20 17:08

1 Answers

In c language, 0 < num1 < 45 cannot be used. You must use 0 < num1 && num1 < 45 using the logical and operator (&&&&).

The && operator is an operator that returns true only when both left and right sides are true. Therefore, 0 < num1 &&& num1 < 45 is true only when num1 is greater than 0 and num1 is less than 45.

Please refer to the code and results below.

#include <stdio.h>

int main()
{
    int num1;
    scanf_s("%d", &num1);

    if (0 < num1 && num1 < 45)
        printf("a");
    else if (90 < num1 && num1 < 160)
        printf("b");
    else
        printf ("None";

    return 0;
}


2022-09-20 17:08

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.