<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://community.arm.com/utility/feedstylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Use of Sprintf increases code size!!</title><link>https://community.arm.com/developer/tools-software/tools/f/keil-forum/20615/use-of-sprintf-increases-code-size</link><description> 
Hi, 
When i use the following code, the code size is as below: 
unsigned char Buf[3]; 
unsigned long oncount = 0; 

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

 
And when i dont use the Sprintf my code size</description><dc:language>en-US</dc:language><generator>Telligent Community 10</generator><item><title>RE: Use of Sprintf increases code size!!</title><link>https://community.arm.com/thread/98356?ContentTypeID=1</link><pubDate>Wed, 16 Aug 2006 12:05:25 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:a5afab43-cf30-4855-befe-5d3ed59821eb</guid><dc:creator>Drew Davis</dc:creator><description>&lt;p&gt;&lt;p&gt;
There are a variety of routines in the standard C library that
convert strings to binary (atoi, strtol, sscanf). But there&amp;#39;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.&lt;/p&gt;

&lt;p&gt;
It&amp;#39;s not that hard to write. Off the top of my head:&lt;/p&gt;

&lt;pre&gt;
// do not call on empty strings; len must be &amp;gt;= 1
void StrReverse (char* s, U8 len)
    {
    char tmpChar;
    U8 i;

    for (i = 0; i &amp;lt; 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++] = &amp;#39;0&amp;#39; + val % 10;
        val /= 10;
        }
    while (val);
    s[len] = 0; // null terminate string

    StrReverse (s, len);

    return len;
    } // U16toString

&lt;/pre&gt;

&lt;p&gt;
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.&lt;/p&gt;

&lt;p&gt;
(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&amp;#39;s another example where both are used. The compiler
could usefully retain both results and avoid having to divide by ten
twice.)&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Use of Sprintf increases code size!!</title><link>https://community.arm.com/thread/74286?ContentTypeID=1</link><pubDate>Wed, 16 Aug 2006 03:04:13 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:f1ddcd39-375e-48c2-a20d-2548d6269f30</guid><dc:creator>Aniket Gokhale</dc:creator><description>&lt;p&gt;&lt;p&gt;
Hi Andy,&lt;/p&gt;

&lt;p&gt;
Thanks for the explanation.&lt;br /&gt;
&lt;i&gt;&amp;quot;Sure - if you just want to create the signed decimal string
representation of an &amp;#39;int&amp;#39;, then write a function which does just
that and nothing else!&amp;quot;&lt;/i&gt;&lt;/p&gt;

&lt;p&gt;
Is there any &lt;b&gt;standard function&lt;/b&gt; available for conversion
from integer to string other than sprintf.&lt;/p&gt;

&lt;p&gt;
I want to take a 3 digit decimal integer value in a string and
display the three decimals.&lt;/p&gt;

&lt;p&gt;
Thanks&lt;/p&gt;

&lt;p&gt;
Aniket&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Use of Sprintf increases code size!!</title><link>https://community.arm.com/thread/46604?ContentTypeID=1</link><pubDate>Wed, 16 Aug 2006 01:45:15 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:cf306ea8-3be7-4bc8-911f-7aa616fd8ca0</guid><dc:creator>Andy Neil</dc:creator><description>&lt;p&gt;&lt;p&gt;
Look at the documentation of sprintf in the &lt;b&gt;manual&lt;/b&gt;: just
the description of all the formats and options runs to several pages
- and then there&amp;#39;s the handling of the variable-length arguments,
parsing the format string, etc...&lt;/p&gt;

&lt;p&gt;
All this versatility &amp;amp; mighty functionality doesn&amp;#39;t come cheap
- it obviously take a lot of code to implement all that stuff!&lt;/p&gt;

&lt;p&gt;
&lt;i&gt;&amp;quot;Is there any alternate for Sprintf ??&amp;quot;&lt;/i&gt;&lt;/p&gt;

&lt;p&gt;
Sure - if you just want to create the signed decimal string
representation of an &amp;#39;int&amp;#39;, then write a function which does just
that and nothing else!&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>