Thread: a problem

  1. #1
    Registered User
    Join Date
    Sep 2012

    a problem

    i have a question: printf("%f",9/5); it prints 0.000000 as output, shouldn't it be 1.000000. Or does it depends on the compiler?

  2. #2
    Registered User
    Join Date
    Sep 2012
    9/5 is an int
    "%f" expects a double

    int and double DO NOT MATCH!: you have Undefined Behaviour.


    printf("%d", 9/5);
    printf("%f", 9/5.0);

  3. #3
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Cambridge, UK
    And also remeber the float can only give a certain amount of pricision as it takes just about machine word to store. If require better precision use double.
    As a fundemental cause of the failure, when you divide two integers you will get an integer. To prodouce a floating result on the operand need to be target result type. As in this case float.

    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sleep() function problem or logic problem?
    By FernandoBasso in forum C Programming
    Replies: 7
    Last Post: 11-16-2011, 05:50 PM
  2. strcmp problem, whats the problem, i cant figure it out!
    By AvaGodess in forum C Programming
    Replies: 14
    Last Post: 10-18-2008, 06:45 PM
  3. Replies: 4
    Last Post: 10-16-2008, 07:30 PM
  4. syntax linked list problem & struct problem
    By beely in forum C Programming
    Replies: 5
    Last Post: 11-11-2002, 09:14 AM
  5. Texture Problem(I got the NeHe tut working, but I have a problem)
    By SyntaxBubble in forum Game Programming
    Replies: 2
    Last Post: 12-02-2001, 10:40 PM