Thread: C code snippet which I need help understanding

  1. #1
    Registered User
    Join Date
    Jul 2006
    Posts
    1

    C code snippet which I need help understanding

    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?

  2. #2
    Registered User Osaou's Avatar
    Join Date
    Nov 2004
    Location
    Stockholm, Sweden
    Posts
    69
    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");

  3. #3
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    715
    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

  4. #4
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help understanding a code
    By lolguy in forum Networking/Device Communication
    Replies: 2
    Last Post: 03-15-2009, 04:13 PM
  2. Why is this code snippet refusing to work ?
    By saraghav in forum C Programming
    Replies: 4
    Last Post: 06-01-2006, 03:25 AM
  3. trouble understanding collision detection code
    By silk.odyssey in forum Game Programming
    Replies: 5
    Last Post: 06-16-2004, 02:27 PM
  4. True ASM vs. Fake ASM ????
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 04-02-2003, 04:28 AM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM