<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Sprintf Issues in Operating System - HP-UX</title>
    <link>https://community.hpe.com/t5/operating-system-hp-ux/sprintf-issues/m-p/5226205#M679432</link>
    <description>If you don't want to put a NUL on the end, you could use sscanf(3) and use %2d.</description>
    <pubDate>Mon, 22 Feb 2010 10:19:22 GMT</pubDate>
    <dc:creator>Dennis Handly</dc:creator>
    <dc:date>2010-02-22T10:19:22Z</dc:date>
    <item>
      <title>Sprintf Issues</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/sprintf-issues/m-p/5226200#M679427</link>
      <description>&lt;!--!*#--&gt;Can some body please help me here. I am just lost. &lt;BR /&gt;&lt;BR /&gt;#include &lt;STDIO.H&gt;&lt;BR /&gt;#include&lt;STDLIB.H&gt;&lt;BR /&gt;main (){&lt;BR /&gt;char mr[18],wmr[18];int imr = 0;char pcs[] = "H5146B2";char pi[]="210023610512918234";&lt;BR /&gt;printf("Id = %s, pcs = %s \n",pi,pcs);&lt;BR /&gt;&lt;BR /&gt;imr = atoi((char *)(strncpy((char *)(mr),(char *)pi+7,2))) + 2000;&lt;BR /&gt;printf("Appende = %s and i value = %s and imr = %d\n",mr,mr,imr);&lt;BR /&gt;&lt;BR /&gt;/*sprintf(mr,"%d",imr);*/&lt;BR /&gt;&lt;BR /&gt;imr = atoi((char *)(strncpy((char *)(mr),(char *)pi+7,2))) + 2000;&lt;BR /&gt;printf("Appendf = %s and i value = %s and imr = %d\n",mr,pi+7,imr);&lt;BR /&gt;&lt;BR /&gt;imr=atoi((char *)(strncpy((char *)(mr),pi+7,2)))+2000;&lt;BR /&gt;printf("Append2 = %s and i value = %d\n",mr,imr);&lt;BR /&gt;}&lt;BR /&gt;&lt;BR /&gt;Output :&lt;BR /&gt;Id = 210023610512918234, pcs = H5146B2&lt;BR /&gt;Appende = 10 and i value = 10 and imr = 2010&lt;BR /&gt;Appendf = 10 and i value = 10512918234 and imr = 2010&lt;BR /&gt;Append2 = 10 and i value = 2010&lt;BR /&gt;&lt;BR /&gt;As soon as I uncomment sprintf &lt;BR /&gt;&lt;BR /&gt;Output : &lt;BR /&gt;Id = 210023610512918234, pcs = H5146B2&lt;BR /&gt;Appende = 10 and i value = 10 and imr = 2010&lt;BR /&gt;Appendf = 1010 and i value = 10512918234 and imr = 3010&lt;BR /&gt;Append2 = 1010 and i value = 3010&lt;BR /&gt;&lt;BR /&gt;I was expecting the value of imr to be 2010 even at 2nd Printf command.&lt;BR /&gt;&lt;BR /&gt;Thanks in advance.&lt;/STDLIB.H&gt;&lt;/STDIO.H&gt;</description>
      <pubDate>Mon, 22 Feb 2010 02:12:13 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/sprintf-issues/m-p/5226200#M679427</guid>
      <dc:creator>Mallik S Pullela</dc:creator>
      <dc:date>2010-02-22T02:12:13Z</dc:date>
    </item>
    <item>
      <title>Re: Sprintf Issues</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/sprintf-issues/m-p/5226201#M679428</link>
      <description>&lt;!--!*#--&gt;&amp;gt; [...] I am just lost.&lt;BR /&gt;&lt;BR /&gt;Well, you may know what you're trying to do,&lt;BR /&gt;which is more than I can say.&lt;BR /&gt;&lt;BR /&gt;Around here, "#include &lt;STRING.H&gt;" would be&lt;BR /&gt;nice, to get strncpy() declared.&lt;BR /&gt;&lt;BR /&gt;Then, in:&lt;BR /&gt;&lt;BR /&gt;      strncpy((char *)(mr),(char *)pi+7,2)&lt;BR /&gt;&lt;BR /&gt;why all the type casting?  And who&lt;BR /&gt;NUL-terminates mr[] after you copy in the two&lt;BR /&gt;characters?&lt;BR /&gt;&lt;BR /&gt;      man strncpy&lt;BR /&gt;&lt;BR /&gt;&amp;gt; /*sprintf(mr,"%d",imr);*/&lt;BR /&gt;&lt;BR /&gt;If "imr" is 2010, then this would tend to set&lt;BR /&gt;mr[] to "2010" (and now it _is_&lt;BR /&gt;NUL-terminated).  Replacing its first two&lt;BR /&gt;characters with "10" (from "pi+7") would seem&lt;BR /&gt;to leave "1010", and around here,&lt;BR /&gt;1010 + 2000 = 3010.&lt;BR /&gt;&lt;BR /&gt;&amp;gt; I was expecting [...]&lt;BR /&gt;&lt;BR /&gt;Why?&lt;BR /&gt;&lt;BR /&gt;Note that on my convenient VMS system, I get&lt;BR /&gt;garbage until someone NUL-terminates "mr":&lt;BR /&gt;&lt;BR /&gt;alp $ type spf2.c&lt;BR /&gt;#include &lt;STDIO.H&gt;&lt;BR /&gt;#include &lt;STDLIB.H&gt;&lt;BR /&gt;#include &lt;STRING.H&gt;&lt;BR /&gt;&lt;BR /&gt;main (){&lt;BR /&gt;char mr[18],wmr[18];int imr = 0;char pcs[] = "H5146B2";char&lt;BR /&gt;pi[]="210023610512918234";&lt;BR /&gt;printf("Id = %s, pcs = %s \n",pi,pcs);&lt;BR /&gt;&lt;BR /&gt;imr = atoi((char *)(strncpy((char *)(mr),(char *)pi+7,2))) + 2000;&lt;BR /&gt;printf("Appende = %s and i value = %s and imr = %d\n",mr,mr,imr);&lt;BR /&gt;&lt;BR /&gt;sprintf(mr,"%d",imr);&lt;BR /&gt;&lt;BR /&gt;imr = atoi((char *)(strncpy((char *)(mr),(char *)pi+7,2))) + 2000;&lt;BR /&gt;printf("Appendf = %s and i value = %s and imr = %d\n",mr,pi+7,imr);&lt;BR /&gt;&lt;BR /&gt;imr=atoi((char *)(strncpy((char *)(mr),pi+7,2)))+2000;&lt;BR /&gt;printf("Append2 = %s and i value = %d\n",mr,imr);&lt;BR /&gt;}&lt;BR /&gt;&lt;BR /&gt;alp $ cc spf2&lt;BR /&gt;alp $ link spf2&lt;BR /&gt;alp $ run spf2&lt;BR /&gt;Id = 210023610512918234, pcs = H5146B2&lt;BR /&gt;Appende = 10Ã¼ and i value = 10Ã¼ and imr = 2010&lt;BR /&gt;Appendf = 1010 and i value = 10512918234 and imr = 3010&lt;BR /&gt;Append2 = 1010 and i value = 3010&lt;BR /&gt;&lt;BR /&gt;You may have luckier garbage than I have, but&lt;BR /&gt;using strncpy() to copy two characters, and&lt;BR /&gt;then not putting in a NUL ('\0') seems to me&lt;BR /&gt;to be rather risky.&lt;BR /&gt;&lt;BR /&gt;If you tried doing less in one statement, and&lt;BR /&gt;looked at what you were feeding into atoi(),&lt;BR /&gt;then you might get some clues as to why your&lt;BR /&gt;code does what it does.&lt;/STRING.H&gt;&lt;/STDLIB.H&gt;&lt;/STDIO.H&gt;&lt;/STRING.H&gt;</description>
      <pubDate>Mon, 22 Feb 2010 04:03:01 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/sprintf-issues/m-p/5226201#M679428</guid>
      <dc:creator>Steven Schweda</dc:creator>
      <dc:date>2010-02-22T04:03:01Z</dc:date>
    </item>
    <item>
      <title>Re: Sprintf Issues</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/sprintf-issues/m-p/5226202#M679429</link>
      <description>Steve, Thank you for pointing it out. &lt;BR /&gt;&lt;BR /&gt;I tried to do too many things in one statment, and I missed the NULL termination of the strcpy guidelines of NULL termination.</description>
      <pubDate>Mon, 22 Feb 2010 06:29:24 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/sprintf-issues/m-p/5226202#M679429</guid>
      <dc:creator>Mallik S Pullela</dc:creator>
      <dc:date>2010-02-22T06:29:24Z</dc:date>
    </item>
    <item>
      <title>Re: Sprintf Issues</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/sprintf-issues/m-p/5226203#M679430</link>
      <description>Steve's answer was correct.</description>
      <pubDate>Mon, 22 Feb 2010 06:30:16 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/sprintf-issues/m-p/5226203#M679430</guid>
      <dc:creator>Mallik S Pullela</dc:creator>
      <dc:date>2010-02-22T06:30:16Z</dc:date>
    </item>
    <item>
      <title>Re: Sprintf Issues</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/sprintf-issues/m-p/5226204#M679431</link>
      <description>&lt;!--!*#--&gt;&amp;gt; [...] NULL termination [...]&lt;BR /&gt;&lt;BR /&gt;NULL is a pointer (typically, (void *) 0).&lt;BR /&gt;NUL is a character ('\0').</description>
      <pubDate>Mon, 22 Feb 2010 06:52:10 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/sprintf-issues/m-p/5226204#M679431</guid>
      <dc:creator>Steven Schweda</dc:creator>
      <dc:date>2010-02-22T06:52:10Z</dc:date>
    </item>
    <item>
      <title>Re: Sprintf Issues</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/sprintf-issues/m-p/5226205#M679432</link>
      <description>If you don't want to put a NUL on the end, you could use sscanf(3) and use %2d.</description>
      <pubDate>Mon, 22 Feb 2010 10:19:22 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/sprintf-issues/m-p/5226205#M679432</guid>
      <dc:creator>Dennis Handly</dc:creator>
      <dc:date>2010-02-22T10:19:22Z</dc:date>
    </item>
  </channel>
</rss>

