Like Tree1Likes
  • 1 Post By qny

a problem

This is a discussion on a problem within the C Programming forums, part of the General Programming Boards category; 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 ...

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    1

    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
    qny
    qny is offline
    Registered User
    Join Date
    Sep 2012
    Posts
    355
    9/5 is an int
    "%f" expects a double

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

    Try

    Code:
    printf("%d", 9/5);
    or
    Code:
    printf("%f", 9/5.0);
    shruthi likes this.

  3. #3
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    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.

    ssharish
    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, 07:45 PM
  3. Replies: 4
    Last Post: 10-16-2008, 08: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21