Code:#include<stdio.h> void main() { float a=0.7; if (0.7>a) printf("Hi"); else printf("Hello"); getch(); }
I get the output Hi .Then what is the purpose of >= symbol.Can you tell me why this is happening?
Code:#include<stdio.h> void main() { float a=0.7; if (0.7>a) printf("Hi"); else printf("Hello"); getch(); }
I get the output Hi .Then what is the purpose of >= symbol.Can you tell me why this is happening?
It's because of how floating point math works.
There is nothing "wrong" with the > operator itself - although you would expect "Hello" to be output in the example above.
If you're interested on the subject, then you might want to read http://en.wikipedia.org/wiki/Floating_point, if not, just know that comparing floating point values is generally a bad idea, and your code above would work "better" like this:
Code:float a = 0.7f; if (a < 0.6999f) printf("Doh"); else printf("Yay");
Here, read this:
http://cboard.cprogramming.com/showt...ighlight=float
Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem
Best try and prevent you from developing other bad habits:
1. main returns int, not void, and although it may seem insignificant, if you
begin posting here regularly, you will be told about it. See my signature for
some info.
2. C is a standardised language, but of course there will always be non
standard code libraries - code that will only run on one type of compiler/OS.
There's nothing wrong with that if your writing code which won't be ported to
another system, but you should know that getch is one such piece of code -
not all compilers will have it, and you have used is incorrectly because you
need to include conio.h - its header file. Just filling you in.
I assume that getch is just to keep the console box open, in which case, look at
this for other methods
Lastly, welcome to the boards!
No No's:
fflush (stdin); gets (); void main ();
Goodies:
Example of fgets (); The FAQ, C/C++ Reference
My Gear:
OS - Windows XP
IDE - MS Visual C++ 2008 Express Edition
ASCII stupid question, get a stupid ANSI