格式化IO; %d或%i? [英] Formatted IO; %d or %i?

查看:89
本文介绍了格式化IO; %d或%i?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

格式化IO中%d和%i之间有什么区别吗?哪一个

是首选?


问候,

八月

Is there any difference between `%d'' and `%i'' in formatted IO? Which one
is preferred?

Regards,
August

推荐答案

在文章< 9g ******************* @ newsb.telia.net>

August Karlstrom< fu********@comhem.se>写道:
In article <9g*******************@newsb.telia.net>
August Karlstrom <fu********@comhem.se> wrote:
格式化IO中%d和%i之间有什么区别吗?哪一个是首选的?
Is there any difference between `%d'' and `%i'' in formatted IO? Which one
is preferred?




对于printf系列功能:没有任何区别。

(我使用% d主要是出于习惯,因为K& RC没有%i。)


对于scanf系列函数:%d和

之间的差异%i与使用strtol()和

基数10(%d)或0(%i)之间的基本相同。也就是说,对于%i,数字0xa0是

有效且值为160十进制,031是八进制,并且具有

值25十进制。 (当然,这也是为什么这么多程序员为什么把b $ b混淆圣诞节和万圣节:12月25日等于10月31日。)

-

In - 实际生活:风河系统Chris Torek

美国犹他州盐湖城(40°39.22''N,111°50.29''W)+1 801 277 2603

电子邮件:忘了它 http://web.torek.net /torek/index.html

由于垃圾邮件发送者,阅读电子邮件就像在垃圾中搜索食物一样。



For the printf family of functions: there is no difference whatsoever.
(I use %d mostly out of habit, because K&R C did not have %i.)

For the scanf family of functions: the difference between %d and
%i is essentially the same as that between using strtol() with a
base of 10 (%d) or 0 (%i). That is, with %i, the number 0xa0 is
valid and has the value 160 decimal, and 031 is octal and has the
value 25 decimal. (This is, of course, why so many programmers
confuse Christmas and Halloween: DEC 25 equals OCT 31.)
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22''N, 111°50.29''W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.


"克里斯托雷克 <无**** @ torek.net>在消息中写道

新闻:d7 ********* @ news2.newsguy.com ...
"Chris Torek" <no****@torek.net> wrote in message
news:d7*********@news2.newsguy.com...
基数为10(%d)或0( %i)。
base of 10 (%d) or 0 (%i).




如何计算基数0?



How does one count in base 0?


Mark写道:
Chris Torek <无**** @ torek.net>在消息中写道
新闻:d7 ********* @ news2.newsguy.com ......
"Chris Torek" <no****@torek.net> wrote in message
news:d7*********@news2.newsguy.com...
基数10(%d)或0( %i)。
base of 10 (%d) or 0 (%i).



如何计算0基数?



How does one count in base 0?




不幸的是,你剪断了相关部分;我会引用它为

参考:

, - Chris Torek:----

|对于scanf系列函数:%d

|之间的差异和%i基本上与使用strtol()

|之间的相同基数为10(%d)或0(%i)。

| [...]

` ----

base 10 / base 0只是引用strtol()的基本参数:

#include< stdlib.h>

long strtol(const char * str,char ** ptr,int base);

ie

sscanf(str,"%i",....)< - > strtol(str,NULL,0)

sscanf(str,"%d",....)< - > strtol(str,NULL,10)


继上述之后,克里斯也解释了差异;在

短,base = 10期望一个基数为10的数字,并将扫描0x57为

0和057为57,而base = 0表示尝试智能猜测

该数字是否为8,10或16。因此,0x57将是57

十六进制,057将是57八进制。

顺便说一句:你不能合理地使用基数1而不是基数为0。

干杯

Michael

-

电子邮件:我的是/ at / gmx / dot / de地址。



Unfortunately, you snipped the relevant part; I''ll quote it for
reference:
,- Chris Torek: ----
| For the scanf family of functions: the difference between %d
| and %i is essentially the same as that between using strtol()
| with a base of 10 (%d) or 0 (%i).
| [...]
`----
base 10/base 0 just refers to the base parameter of strtol():
#include <stdlib.h>
long strtol(const char *str, char **ptr, int base);
i.e.
sscanf(str, "%i", ....) <-> strtol(str, NULL, 0)
sscanf(str, "%d", ....) <-> strtol(str, NULL, 10)

Following the above, Chris also explained the difference; in
short, base=10 expects a base 10 number and will scan 0x57 as
0 and 057 as 57, whereas base=0 means "try to intelligently guess
whether the number is base 8, 10, or 16". So, 0x57 will be 57
hexadecimal and 057 will be 57 octal.
BTW: You cannot work sensibly with base 1 and not at all with base 0.
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.


这篇关于格式化IO; %d或%i?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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