help with sprintf

This is a discussion on help with sprintf within the C Programming forums, part of the General Programming Boards category; Im new to C and simply want to format a floating point number but i keep getting the wrong output. ...

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    7

    help with sprintf

    Im new to C and simply want to format a floating point number but i keep getting the wrong output.

    Code:
    char DiamString[10];
    
    void AddDiam (float diameter, void *LineNum)
    {//Takes the diameter and stores it to the 2nd line
    
        unsigned char *p1;
        p1=LineNum;
    
        sprintf(DiamString,"%2.1f",diameter);
        strcpy (p1+8,DiamString);
    
    }
    When i pass diameter = 13.45 I get DiamString = 10.0, diameter = 29.44 DiamString = 20.0, and diameter = 34.31 DiamString = 30.0.
    Why is this happening??

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    The title says about sprintf, so I run it with focusing only on this and got the correct result
    Code:
    #include <stdio.h>
    
    char DiamString[10];
     
    void AddDiam (float diameter)
    {//Takes the diameter and stores it to the 2nd line
     
        sprintf(DiamString,"%f",diameter);
        
     
    }
    int main () {
        AddDiam(13.45);
        printf("%s\n", DiamString);
        return 0;
    }
    If you use 2.1 in the format you simply get less precision
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  3. #3
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,625
    "%2.1f" means "output a total of 2 characters ( decimal point included ), and 1 character after the decimal point"
    But because 13.45 for example has 2 most significant digits, it prints them "13" and it's already done! But it HAS to print the decimal point and places, therefore it prints "13.0"
    Devoted my life to programming...

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    The code does not do what you say it does, so there is a problem with your assumptions, and the cause is within other code that you have not shown.

    Why is there a plus 8 in there?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 02-12-2010, 08:02 PM
  2. sprintf
    By Bladactania in forum C Programming
    Replies: 1
    Last Post: 02-13-2009, 12:08 PM
  3. sprintf(len+id ?
    By ReggieBE in forum C Programming
    Replies: 2
    Last Post: 05-25-2007, 08:46 PM
  4. sprintf
    By Gordon in forum Windows Programming
    Replies: 8
    Last Post: 04-27-2007, 03:06 PM
  5. SPRINTF help please.
    By Astra in forum C Programming
    Replies: 7
    Last Post: 11-02-2006, 06:32 AM

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