I have a problem geeks!!! In following program.....

#include<stdio.h>
#include<conio.h>
void main()
{
  float f=0.7;
  clrscr();  
  if(f<0.7)
     printf("C+");
  else
     printf("C++");
  getch();
}

in the above program when I give the value of f=0.7 and in if statement also I put the same value 0.7 then the output of these codes come to be "C+" whether I was expecting "C++" for this...........on the other hand if I make f=(something other than 0.7) like 0.5 or 0.8 and also put the same value in if statement then it is working as per my expectations and giving the output as "C++"

Recommended Answers

All 8 Replies

We do not like to be referred to as "geeks"

And basicly it does not matter what you put in the if statement because you are saying

if(f<0.7)
cout << "C+";

else <- anything else at all
cout << "C++";

So basicly if you input a number less than 0.7 you would get C+, else any number eg 0.7,7,77 etc etc
would give you a outout of C++

EDIT: you should change void main() -> int main()
clrscr is undeclared,

seriously you need to modify your code

commented: Well said! +15

I dont mind being called a geek because that is more or less what I am :)

Note that the '==' operator does not apply to float. You would get the output as C++ if you tried

if(f==.7)

@BLACK MAGIC
perhaps my problem is not clear to you all............at first look at this code

#include<stdio.h>
      #include<conio.h>
      void main()
      {
         float f=0.7;
         clrscr();
         if(f<0.7)
            printf("C+");
         else
            printf("C++");
         getch();
      }

this code should give the output as "C++" but this gives output as "C+"...but on the other hand if I change the value of float to 0.5(or any other value) at both places as in following code

#include<stdio.h>
      #include<conio.h>
      void main()
      {
         float f=0.5;
         clrscr();
         if(f<0.5)
            printf("C+");
        else
            printf("C++");
       getch();
        }

then it gives the output "C++" as expected. so I wanted to know the reason behind this.

PS:I'm using C....and void is used b'coz main function is not returning any value. and I've taken header file conio.h in which clrscr() is declared.

main always returns a value whether you like it or not, no matter whether you declare void main, or whether your compiler doesn't complain about it.

http://c-faq.com/ansi/maindecl.html

> so I wanted to know the reason behind this.
You're comparing a float to a double.
In the context of the if statement, f is promoted to a double, AND IN DOING SO changes value just enough to cause a shed-load of confusion. The 0.7 in your if statement is actually a double constant.
Writing if ( f < 0.7f ) might help in this instance, but doesn't change the underlying problems with the code.

0.5 (being a power of 2) can be stored accurately in a floating point number, so converting from float to double doesn't lose anything. But 0.7 is an approximation which gets even more imprecise with the conversion. You have to understand that < doesn't understand anything like "close enough".

http://en.wikipedia.org/wiki/Floating_point
http://docs.sun.com/source/806-3568/ncg_goldberg.html

oh ya! thnx. I got my answer.....thank you all.

can you tell me how to mark my thread as salved plz.......

There's a link (either at the top or bottom of your thread) which should be "mark solved".
It might be on a pull-down menu at the top of the thread.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.