c - If statement being ignored

by herondale   Last Updated September 12, 2019 02:26 AM

So I'm new to c and I'm running into an issue where my if statement is being completely ignored. I want the program to accept two float values that are equal to or larger than 10 and exit with an error message if it does not meet these requirements.

It seems that whatever numbers I enter, the program always rejects the if statement and accepts the else statement. What am I doing wrong?

#include <stdio.h>
#include <stdlib.h>

int main()
{
    float first;
    float second;
    printf("Enter a number \n");
    scanf("%f", first);
    printf("Enter another number \n");
    scanf("%f", second);

    if((first >= 10) && (second >= 10)){
        printf("thanks");
    }else{
        printf("enter a larger number");
    }

    return 0;
}
Tags : c if-statement


Answers 2


This is because you are trying to compare the float value with an integer int . I.e float can not be campared with int and that's why your if block is skipped every time.

To avoid this you can simply typecast the int into float for this comparison by appending f in the last, and it will work.

First<10f

Mukul Kumar Jha
Mukul Kumar Jha
September 12, 2019 02:18 AM

You need to add a & to the two scanf's, as in:

scanf("%f", &first)
Joseph Quinsey
Joseph Quinsey
September 12, 2019 02:24 AM

Related Questions





convert if-else to switch statements java

Updated March 21, 2019 08:26 AM