scanf(“%i”)和scanf(“%d”)之间的差异???可能是VS2005中的错误? [英] difference between scanf("%i") and scanf("%d") ??? perhaps bug inVS2005?

查看:55
本文介绍了scanf(“%i”)和scanf(“%d”)之间的差异???可能是VS2005中的错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




今天我遇到了一个非常奇怪的问题......我自己做了一个数据文件和

我读了数据从那个文件....


当我读到类似这样的内容时:


03 04 05,00 04 01,05 03 07 ,08 03 00,09 06 03 ...等


与scanf(%i%i%i,"& var1,& var2, & var3);


然后它停止读取以08,09开头的数据。等...但是

所有诸如07之类的东西。以下工作...


然后我尝试了scanf(%d%d%d,"& var1,& var2,& var3);


节目工作了!?!!


为什么?


我看着我的C -book和它说使用%i绝对相同

%d ....我太累了,不能减少我的程序并发布一个小版本的

现在,它可能是'甚至不必要因为有人知道

发生了什么......


编译器:Visual studio 2005年,windows xp。我怀疑也许这是一个错误,

如果我的C书是对的,%i和%d之间没有任何区别?

祝你好运/ Med venlig hilsen

Martin J?rgensen


-

-------------- -------------------------------------------------- -----------

Martin J?rgensen的家 - http://www.martinjoergensen.dk

推荐答案

" Martin J?rgensen" <未********* @ spam.jay.net>在留言中写道

news:v3 ************ @ news.tdc.dk ...
"Martin J?rgensen" <un*********@spam.jay.net> wrote in message
news:v3************@news.tdc.dk...
今天我得到了一个非常奇怪的问题......我已经把自己变成了一个数据文件,我从那个文件中读取数据......

当我读到这样的内容时:

03 04 05,00 04 01,05 03 07,08 03 00,09 06 03 ......等等

像scanf一样(&%;%i%i %i,"& var1,& var2,& var3);
然后它停止读取以08,09开头的数据。等...但是诸如07之类的所有内容都是如此。以下工作...

然后我尝试了scanf(%d%d%d,"& var1,& var2,& var3);

为什么?

我查看了我的C-book,它说使用%i作为
%d ....我太累了,不能减少我的程序并发布它的小版本
现在,这可能是'甚至没有必要因为有人知道'是什么'发生了......

编译器:Visual Studio 2005,windows xp。我怀疑也许这是一个错误,如果我的C书是对的,%i和%d之间没有任何区别吗?
Today I got a really strange problem... I''ve made myself a data-file and I
read in data from that file....

When I read something like this line:

03 04 05, 00 04 01, 05 03 07, 08 03 00, 09 06 03 ... etc.

with something like scanf("%i %i %i, ", &var1, &var2, &var3);

then it stopped reading data starting with "08", "09" etc... But
everything such as "07" and below worked...

Then I tried scanf("%d %d %d, ", &var1, &var2, &var3);

And the program WORKED!?!!?

Why?

I looked in my C-book and it says absolutetly the same about using %i as
%d.... I''m too tired to cut down my progrm and post a small version of it
now, and it might be that''s even unnecessary because somebody know what''s
happening...

Compiler: Visual studio 2005, windows xp. I suspect perhaps it''s a bug, if
my C book is right that there isn''t any difference between %i and %d?




%d有效地调用strtol,其基数为10,因此它假定所有数字

它读取的是十进制数。 %d有效地调用strtol,其基数为0,因此它根据它看到的任何前缀(0,0x或0X)来调整基数。因此08

是一个形成错误的八进制整数。


HTH,


PJ Plauger

Dinkumware,Ltd。
http://www.dinkumware.com


PJ Plauger写道:
P.J. Plauger wrote:
" Martin J?rgensen" <未********* @ spam.jay.net>在消息中写道
新闻:v3 ************ @ news.tdc.dk ...
"Martin J?rgensen" <un*********@spam.jay.net> wrote in message
news:v3************@news.tdc.dk...
今天我遇到了一个非常奇怪的问题...我已经把自己变成了一个数据文件,然后我从那个文件中读取数据....

当我读到这样的内容时:

03 04 05,00 04 01,05 03 07,08 03 00,09 06 03 ...等等

类似于scanf(%i%i%i, ;& var1,& var2,& var3);
然后它停止读取以08,09开头的数据。等...但是诸如07之类的所有内容都是如此。以下工作...

然后我尝试了scanf(%d%d%d,"& var1,& var2,& var3);

为什么?

我查看了我的C-book,它说使用%i作为
%d ....我太累了,不能减少我的程序并发布它的小版本
现在,这可能是'甚至没有必要因为有人知道'是什么'发生了......

编译器:Visual Studio 2005,windows xp。我怀疑也许这是一个错误,如果我的C书是正确的,%i和%d之间没有任何区别?
Today I got a really strange problem... I''ve made myself a data-file and I
read in data from that file....

When I read something like this line:

03 04 05, 00 04 01, 05 03 07, 08 03 00, 09 06 03 ... etc.

with something like scanf("%i %i %i, ", &var1, &var2, &var3);

then it stopped reading data starting with "08", "09" etc... But
everything such as "07" and below worked...

Then I tried scanf("%d %d %d, ", &var1, &var2, &var3);

And the program WORKED!?!!?

Why?

I looked in my C-book and it says absolutetly the same about using %i as
%d.... I''m too tired to cut down my progrm and post a small version of it
now, and it might be that''s even unnecessary because somebody know what''s
happening...

Compiler: Visual studio 2005, windows xp. I suspect perhaps it''s a bug,if
my C book is right that there isn''t any difference between %i and %d?



%d有效地调用strtol的基数为10,所以它假设所有数字都是十进制的。 %d有效地调用strtol,其基数为0,因此它根据它看到的任何前缀(0,0x或0X)调整基数。因此08
是一个形成错误的八进制整数。



%d effectively calls strtol with a base of 10, so it assumes all numbers
it reads are decimal. %d effectively calls strtol with a base of 0, so it
adapts the base according to any prefix (0, 0x, or 0X) it sees. Thus 08
is an ill formed octal integer.




你的第二个%d应该是%i。


Robert Gamble



Your second %d should be a %i.

Robert Gamble


" PJ Plauger" < pj*@dinkumware.com>写道:
"P.J. Plauger" <pj*@dinkumware.com> writes:
" Martin J?rgensen" <未********* @ spam.jay.net>在消息中写道
新闻:v3 ************ @ news.tdc.dk ...
"Martin J?rgensen" <un*********@spam.jay.net> wrote in message
news:v3************@news.tdc.dk...
今天我遇到了一个非常奇怪的问题......我我自己制作了一个数据文件,然后我从那个文件中读取数据....

当我读到这样的内容时:

03 04 05,00 04 01,05 03 07,08 03 00,09 06 03 ...等等

像scanf(%i%i%i,,& var1,& var2,& var3);
然后它停止读取以08,09开头的数据。等...但是诸如07之类的所有内容都是如此。以下工作...

然后我尝试了scanf(%d%d%d,"& var1,& var2,& var3);
程序工作!?!!?
Today I got a really strange problem... I''ve made myself a data-file and I
read in data from that file....

When I read something like this line:

03 04 05, 00 04 01, 05 03 07, 08 03 00, 09 06 03 ... etc.

with something like scanf("%i %i %i, ", &var1, &var2, &var3);

then it stopped reading data starting with "08", "09" etc... But
everything such as "07" and below worked...

Then I tried scanf("%d %d %d, ", &var1, &var2, &var3);

And the program WORKED!?!!?


[snip

%d有效地调用strtol,基数为10,所以它假定所有数字
它读数是十进制的。 %d有效地调用strtol,其基数为0,因此它根据它看到的任何前缀(0,0x或0X)调整基数。因此08
是一个错误形成的八进制整数。

[snip
%d effectively calls strtol with a base of 10, so it assumes all numbers
it reads are decimal. %d effectively calls strtol with a base of 0, so it
adapts the base according to any prefix (0, 0x, or 0X) it sees. Thus 08
is an ill formed octal integer.




并且%d,010值为8,而不是10.


决定是否要将带有0的数字视为

八进制或十进制。 />

-

Keith Thompson(The_Other_Keith) ks***@mib.org < http://www.ghoti.net/~kst>

圣地亚哥超级计算机中心< *> < http://users.sdsc.edu/~kst>

我们必须做点什么。这是事情。因此,我们必须这样做。



And with %d, "010" has the value 8, not 10.

Decide whether you want numbers with leading ''0'' to be treated as
octal or decimal.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.


这篇关于scanf(“%i”)和scanf(“%d”)之间的差异???可能是VS2005中的错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆