﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-yx-随笔分类-C++</title><link>http://www.cppblog.com/csu-yx-2013/category/18360.html</link><description>Algorithm Study And So On</description><language>zh-cn</language><lastBuildDate>Fri, 13 Sep 2013 01:08:21 GMT</lastBuildDate><pubDate>Fri, 13 Sep 2013 01:08:21 GMT</pubDate><ttl>60</ttl><item><title>Dev-C++下使用scanf("lld", nX)读取long long时出现的bug</title><link>http://www.cppblog.com/csu-yx-2013/archive/2011/12/11/161927.html</link><dc:creator>yx</dc:creator><author>yx</author><pubDate>Sun, 11 Dec 2011 10:20:00 GMT</pubDate><guid>http://www.cppblog.com/csu-yx-2013/archive/2011/12/11/161927.html</guid><wfw:comment>http://www.cppblog.com/csu-yx-2013/comments/161927.html</wfw:comment><comments>http://www.cppblog.com/csu-yx-2013/archive/2011/12/11/161927.html#Feedback</comments><slash:comments>11</slash:comments><wfw:commentRss>http://www.cppblog.com/csu-yx-2013/comments/commentRss/161927.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/csu-yx-2013/services/trackbacks/161927.html</trackback:ping><description><![CDATA[<div>&nbsp; &nbsp;首先，要再现bug得先准备bug条件，使用Windows下的Dev-C++按照目录下bin文件夹下面的c和c++编译器和链接器，可以直接使用Dev-C++，或者用CodeBlocks然后编译链接的目录设置为Dev-C++的bin目录。这个bug是在今天月赛时候出现的，我用%lld读入一个不大的整数后，再for循环读入其它一些数字，发现无论如何输出不了，而我用cin和cout操作longlong的时候，超时1倍多，很恶心的出题人，本来就是一个水题，居然做成这样。然后没办法，快结束的时候，问旁边的队友，因为是个人赛，所以是自己在做，longlong，如何读入，他说%lld。是啊，我一直这样读，这样输出，为啥出问题了。。。没办法照着他的样子，把输入改成了int，直接%d读入，答案还是longlong，再%lld输出就没超时了，真恶心的一天啊。&nbsp; &nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;64位本来就用得不多，而且对于大多数Windows下的用户，基本都是vc6和vs08什么的。vs08我已经实验过，不会出现这个bug，PS：是完全一样的代码，亲自单步调试实验的，无任何bug。vc6只能用%I64d输入和输出。那么，问题就只是在Dev-C++的用户中存在了。&nbsp; &nbsp;回来的时候，我就决心找出问题的所在。所以，我打算升级g++的版本。下了个Dev-C++ 5.0也没用，和前面的Dev-C++ 4.9.9.2一样的，恶心啊。<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;然后google+百度了很久，发现CSDN上一篇博文解释说，这就是Dev-C++自己的事情。因为gcc本来是linux下的，所以longlong在自己家里是不会出现问题的。而Dev-C++是把人家移植过来的，那篇博文说Dev-C++的编译和链接器是mingw32-g++.exe，但是Mingw32在编译期间使用gcc的规则检查语法，在连接和运行时使用的却是Microsoft库。这个库里的printf和scanf函数当然不认识linux gcc下"%lld"和"%llu"，对"%I64d"和"%I64u"，它则是乐意接受的。Mingw32在编译期间使用gcc的规则检查语法，在连接和运行时使用的却是Microsoft库。这个库里的printf和scanf函数当然不认识linux gcc下"%lld"和"%llu"，对"%I64d"和"%I64u"，它则是乐意接受的。意思是，程序里面实质的二进制代码可能是微软的库，只解析%I64d,然后就可能出错了。具体是什么原因，只有开发Dev-C++的人知道了。或者其它高人。。。<br /><br /><div><div style="background-color: #eeeeee; font-size: 13px; ">#include&nbsp;&lt;stdio.h&gt;</div>#include&nbsp;&lt;iostream&gt;<br style="background-color: #eeeeee; " /><span style="background-color: #eeeeee; font-size: 13px; color: #0000ff; ">using</span>&nbsp;<span style="background-color: #eeeeee; font-size: 13px; color: #0000ff; ">namespace</span>&nbsp;std;<br style="background-color: #eeeeee; " /><br style="background-color: #eeeeee; " /><span style="background-color: #eeeeee; font-size: 13px; color: #0000ff; ">int</span>&nbsp;main()<br style="background-color: #eeeeee; " />{<br style="background-color: #eeeeee; " />&nbsp;&nbsp;&nbsp;&nbsp;<span style="background-color: #eeeeee; font-size: 13px; color: #0000ff; ">long</span>&nbsp;<span style="background-color: #eeeeee; font-size: 13px; color: #0000ff; ">long</span>&nbsp;nN;<br style="background-color: #eeeeee; " />&nbsp;&nbsp;&nbsp;&nbsp;<span style="background-color: #eeeeee; font-size: 13px; color: #0000ff; ">long</span>&nbsp;<span style="background-color: #eeeeee; font-size: 13px; color: #0000ff; ">long</span>&nbsp;nX,&nbsp;nY;<br style="background-color: #eeeeee; " /><br style="background-color: #eeeeee; " />&nbsp;&nbsp;&nbsp;&nbsp;<span style="background-color: #eeeeee; font-size: 13px; color: #0000ff; ">if</span>&nbsp;(scanf("%lld",&nbsp;&amp;nN)&nbsp;!=&nbsp;EOF)<br style="background-color: #eeeeee; " />&nbsp;&nbsp;&nbsp;&nbsp;{<br style="background-color: #eeeeee; " />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf("nN:%lld\n",&nbsp;nN);<br style="background-color: #eeeeee; " />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br style="background-color: #eeeeee; " />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="background-color: #eeeeee; font-size: 13px; color: #0000ff; ">for</span>&nbsp;(<span style="background-color: #eeeeee; font-size: 13px; color: #0000ff; ">long</span>&nbsp;<span style="background-color: #eeeeee; font-size: 13px; color: #0000ff; ">long</span>&nbsp;i&nbsp;=&nbsp;0;&nbsp;i&nbsp;&lt;&nbsp;nN;&nbsp;++i)<br style="background-color: #eeeeee; " />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br style="background-color: #eeeeee; " />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf("nN:%lld&nbsp;i:%lld\n",&nbsp;nN,&nbsp;i);<br style="background-color: #eeeeee; " />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br style="background-color: #eeeeee; " />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;getchar();<br style="background-color: #eeeeee; " />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf("Over\n");<br style="background-color: #eeeeee; " />&nbsp;&nbsp;&nbsp;&nbsp;}<br style="background-color: #eeeeee; " /><br style="background-color: #eeeeee; " />&nbsp;&nbsp;&nbsp;&nbsp;<span style="background-color: #eeeeee; font-size: 13px; color: #0000ff; ">return</span>&nbsp;0;<br style="background-color: #eeeeee; " />}<br style="background-color: #eeeeee; " /><br style="background-color: #eeeeee; " /><br style="background-color: #eeeeee; " />该代码会一直死循环，大家可以试试<br style="background-color: #eeeeee; " /><img src="http://www.cppblog.com/images/cppblog_com/csu-yx/DevClonglong的bug1.jpg" width="180" height="426" alt="" /><br style="background-color: #eeeeee; " /><br style="background-color: #eeeeee; " />如果改成下面这样，还可以看到输入的数据都没有到达指定的变量<br style="background-color: #eeeeee; " /><div style="background-color: #eeeeee; "><div>#include &lt;stdio.h&gt;</div><div>#include &lt;iostream&gt;</div><div>using namespace std;</div><div></div><div>int main()</div><div>{</div><div>&nbsp; &nbsp; long long nN;</div><div>&nbsp; &nbsp; long long nX, nY;</div><div>&nbsp; &nbsp;&nbsp;</div><div>&nbsp; &nbsp; if (scanf("%lld", &amp;nN) != EOF)</div><div>&nbsp; &nbsp; {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; printf("nN:%lld\n", nN);</div><div>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; for (long long i = 0; i &lt; nN; ++i)</div><div>&nbsp; &nbsp; &nbsp; &nbsp; {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; printf("nN:%lld i:%lld\n", nN, i);</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; scanf("%lld %lld", &amp;nX, &amp;nY);</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; printf("nX:%lld nY:%lld\n", nX, nY);</div><div>&nbsp; &nbsp; &nbsp; &nbsp; }</div><div>&nbsp; &nbsp; &nbsp; &nbsp; getchar();</div><div>&nbsp; &nbsp; &nbsp; &nbsp; printf("Over\n");</div><div>&nbsp; &nbsp; }</div><div></div><div>&nbsp; &nbsp; return 0;</div><div>}</div></div><div style="background-color: #eeeeee; font-size: 13px; "><img src="http://www.cppblog.com/images/cppblog_com/csu-yx/DevClonglong的bug2.jpg" width="225" height="431" alt="" /></div></div></div><img src ="http://www.cppblog.com/csu-yx-2013/aggbug/161927.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/csu-yx-2013/" target="_blank">yx</a> 2011-12-11 18:20 <a href="http://www.cppblog.com/csu-yx-2013/archive/2011/12/11/161927.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>