C++ 格式化 IO 函数中的转换说明符 %i 和 %d 有什么区别(*printf / *scanf)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1893490/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 21:29:54  来源:igfitidea点击:

What is the difference between conversion specifiers %i and %d in formatted IO functions (*printf / *scanf)

c++cprintfscanfformat-specifiers

提问by Ayoub M.

What is the difference between %dand %iwhen used as format specifiers in printf?

在 中用作格式说明符%d%i用作格式说明符时有什么区别printf

回答by Dipstick

They are the same when used for output, e.g. with printf.

它们在用于输出时是相同的,例如使用printf.

However, these are different when used as input specifier e.g. with scanf, where %dscans an integer as a signed decimal number, but %idefaults to decimal but also allows hexadecimal (if preceded by 0x) and octal (if preceded by 0).

但是,当用作输入说明符时,这些是不同的,例如 with scanf,其中%d将整数扫描为带符号的十进制数,但%i默认为十进制但也允许十六进制(如果前面有0x)和八进制(如果前面有0)。

So 033would be 27 with %ibut 33 with %d.

所以033将是 27,%i但 33 与%d.

回答by jason

These are identical for printfbut different for scanf. For printf, both %dand %idesignate a signed decimal integer. For scanf, %dand %ialso means a signed integer but %iinteprets the input as a hexadecimal number if preceded by 0xand octal if preceded by 0and otherwise interprets the input as decimal.

这些对于 是相同的,printf但对于是不同的scanf。对于printf,%d和 都%i指定一个有符号十进制整数。为scanf%d并且%i也意味着带符号的整数,但%iinteprets如果前面由输入为十六进制数0x和八进制如果前面由0否则解释输入为十进制。

回答by Shafik Yaghmour

There is no difference between the %iand %dformat specifiers for printf. We can see this by going to the draft C99 standardsection 7.19.6.1The fprintf functionwhich also covers printfwith respect to format specifiers and it says in paragraph 8:

%i%d格式说明符之间没有区别printf。我们可以通过转到C99 标准草案部分看到这一点7.19.6.1。 fprintf 函数也涵盖printf了格式说明符,它在第8段中说:

The conversion specifiers and their meanings are:

转换说明符及其含义是:

and includes the following bullet:

并包括以下项目符号:

d,i     The int argument is converted to signed decimal in the style
        [?]dddd. The precision specifies the minimum number of digits to
        appear; if the value being converted can be represented in fewer
        digits, it is expanded with leading zeros. The default precision is
        1. The result of converting a zero value with a precision of zero is
        no characters.
d,i     The int argument is converted to signed decimal in the style
        [?]dddd. The precision specifies the minimum number of digits to
        appear; if the value being converted can be represented in fewer
        digits, it is expanded with leading zeros. The default precision is
        1. The result of converting a zero value with a precision of zero is
        no characters.

On the other hand for scanfthere is a difference, %dassume base 10 while %iauto detects the base. We can see this by going to section 7.19.6.2The fscanf functionwhich covers scanfwith respect to format specifier, in paragraph 12it says:

另一方面,由于scanf存在差异,%d假设基数为 10,而%i自动检测基数。我们可以通过将部分看到这个7.19.6.2fscanf函数覆盖scanf相对于格式说明,在第12这样说的:

The conversion specifiers and their meanings are:

转换说明符及其含义是:

and includes the following:

并包括以下内容:

d     Matches an optionally signed decimal integer, whose format is the
      same as expected for the subject sequence of the strtol function with
      the value 10 for the base argument. The corresponding argument shall
      be a pointer to signed integer.

i     Matches an optionally signed integer, whose format is the same as
      expected for the subject sequence of the strtol function with the
      value 0 for the base argument. The corresponding argument shall be a
      pointer to signed integer.
d     Matches an optionally signed decimal integer, whose format is the
      same as expected for the subject sequence of the strtol function with
      the value 10 for the base argument. The corresponding argument shall
      be a pointer to signed integer.

i     Matches an optionally signed integer, whose format is the same as
      expected for the subject sequence of the strtol function with the
      value 0 for the base argument. The corresponding argument shall be a
      pointer to signed integer.

回答by Shafik Yaghmour

There isn't any in printf- the two are synonyms.

没有 in printf- 两者是同义词。