Relational operator in C while using real variables

483 Views Asked by At

Why the output is not as expected?

#include <stdio.h>
void main(){
    float a,b,c;
    b=0.7;
    if( b<0.7 )
        printf(" It should NOT be here");
    else
        printf("It Should be here");
}
3

There are 3 best solutions below

1
On

Floating point numbers

So behaviour You are surprised

7
On

0.7 is double value. Try 0.7f in code. It should work.

9
On

Please Try the below Code, it works!!!:

Code

 #include <stdio.h>

    int  main(void)
    {

    float a,b,c,temp;
    temp=0.7;
    b=0.7;

    if( b<temp )
        printf(" It should NOT be here");
    else
        printf("It Should be here");


    }