Boost C++ Libraries: Ticket #892: boost::counting_iterator https://svn.boost.org/trac10/ticket/892 <pre class="wiki">The following example code boost::counting_iterator&lt;int&gt; pi1(3); boost::counting_iterator&lt;int&gt; pi2(7); printf("diff %d, %d\n", pi2 - pi1, 5); should print diff 4, 5 but instead wrongly prints diff 4, 0 Tested with gcc. </pre> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/892 Trac 1.4.3 ljj116 Fri, 13 Apr 2007 18:47:21 GMT <link>https://svn.boost.org/trac10/ticket/892#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/892#comment:1</guid> <description> <pre class="wiki">Logged In: YES user_id=1766784 Originator: NO It's because the subtraction results in a 64-bit number. %d is only expecting 32 bits, so the first 32 bits from the stack goes to the first %d and the second 32 bits goes to the second %d. The third 32-bits (5) is never accessed by the printf function. If you really want to do that, use: printf("diff %d, %d\n", (int)(pi2 - pi1), 5); The same thing happens if you use this code on a machine where long long is 64 bits: long long my64bitnum = 4; printf("diff %d, %d\n", my64bitnum, 5); </pre> </description> <category>Ticket</category> </item> <item> <dc:creator>ljj116</dc:creator> <pubDate>Fri, 13 Apr 2007 18:53:12 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/892#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/892#comment:2</guid> <description> <pre class="wiki">Logged In: YES user_id=1766784 Originator: NO or printf("diff %lld, %d\n", pi2 - pi1, 5); if you want all 64 bits. </pre> </description> <category>Ticket</category> </item> <item> <dc:creator>david_abrahams</dc:creator> <pubDate>Fri, 13 Apr 2007 21:33:36 GMT</pubDate> <title>status changed https://svn.boost.org/trac10/ticket/892#comment:3 https://svn.boost.org/trac10/ticket/892#comment:3 <ul> <li><strong>status</strong> <span class="trac-field-old">assigned</span> → <span class="trac-field-new">closed</span> </li> </ul> <pre class="wiki">Logged In: YES user_id=52572 Originator: NO Closing since this is clearly not a bug in counting_iterator. Use C++ iostreams or Boost.Format if you want typesafe I/O. </pre> Ticket