This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Use of Sprintf increases code size!!

Hi,
When i use the following code, the code size is as below:
unsigned char Buf[3];
unsigned long oncount = 0;

sprintf (Buf,"%d",(int)(oncount/360000));
Program Size: data=45.1 xdata=1 code=1847

And when i dont use the Sprintf my code size gets reduced as below:
Program Size: data=24.0 xdata=1 code=558

Is there any alternate for Sprintf ??

Aniket

  • Look at the documentation of sprintf in the manual: just the description of all the formats and options runs to several pages - and then there's the handling of the variable-length arguments, parsing the format string, etc...

    All this versatility & mighty functionality doesn't come cheap - it obviously take a lot of code to implement all that stuff!

    "Is there any alternate for Sprintf ??"

    Sure - if you just want to create the signed decimal string representation of an 'int', then write a function which does just that and nothing else!

  • Hi Andy,

    Thanks for the explanation.
    "Sure - if you just want to create the signed decimal string representation of an 'int', then write a function which does just that and nothing else!"

    Is there any standard function available for conversion from integer to string other than sprintf.

    I want to take a 3 digit decimal integer value in a string and display the three decimals.

    Thanks

    Aniket

  • There are a variety of routines in the standard C library that convert strings to binary (atoi, strtol, sscanf). But there's a dearth of routines that go the other way, integer to string. Routiens like itoa() are common extensions, but not actually part of the C89 standard, and not supplied with the Keil libraries.

    It's not that hard to write. Off the top of my head:

    // do not call on empty strings; len must be >= 1
    void StrReverse (char* s, U8 len)
        {
        char tmpChar;
        U8 i;
    
        for (i = 0; i < len / 2; ++i)
            {
            tmpChar = s[i];
            s[i] = s[len - 1 - i];
            s[len - 1 - i] = tmpChar;
            }
    
        } // StrReverse
    
    
    
    U8 U16toString (U16 val, char* s)
        {
        U8 len;
    
        len = 0;
        do
            {
            s[len++] = '0' + val % 10;
            val /= 10;
            }
        while (val);
        s[len] = 0; // null terminate string
    
        StrReverse (s, len);
    
        return len;
        } // U16toString
    
    

    You could also build the string most-significant-digit first, but it seemed to me like there would be a lot more division and multiplication involved that way, and that reversing the result would be faster.

    (Once upon a time, probably years ago now, Jon Ward asked if there was any reason people would use both quotient and remainder of a mod operation. Here's another example where both are used. The compiler could usefully retain both results and avoid having to divide by ten twice.)