prinf formatting problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sanganaksakha
    New Member
    • Mar 2007
    • 1

    prinf formatting problem

    printf("%d", 12.0 / 8);

    Why does the above print 0 and not 1.

    - SSK
  • horace1
    Recognized Expert Top Contributor
    • Nov 2006
    • 1510

    #2
    Originally posted by sanganaksakha
    printf("%d", 12.0 / 8);

    Why does the above print 0 and not 1.

    - SSK
    the expression 12.0 / 8 yeilds a double result which you attempt to print as a int using the %d conversion specification. If you cast the result to an int it will print correctly, e.g.
    Code:
    printf("%d", (int) (12.0 / 8));

    Comment

    Working...