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
What is the difference between conversion specifiers %i and %d in formatted IO functions (*printf / *scanf)
提问by Ayoub M.
What is the difference between %d
and %i
when 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 %d
scans an integer as a signed decimal number, but %i
defaults to decimal but also allows hexadecimal (if preceded by 0x
) and octal (if preceded by 0
).
但是,当用作输入说明符时,这些是不同的,例如 with scanf
,其中%d
将整数扫描为带符号的十进制数,但%i
默认为十进制但也允许十六进制(如果前面有0x
)和八进制(如果前面有0
)。
So 033
would be 27 with %i
but 33 with %d
.
所以033
将是 27,%i
但 33 与%d
.
回答by jason
These are identical for printf
but different for scanf
. For printf
, both %d
and %i
designate a signed decimal integer. For scanf
, %d
and %i
also means a signed integer but %i
inteprets the input as a hexadecimal number if preceded by 0x
and octal if preceded by 0
and otherwise interprets the input as decimal.
这些对于 是相同的,printf
但对于是不同的scanf
。对于printf
,%d
和 都%i
指定一个有符号十进制整数。为scanf
,%d
并且%i
也意味着带符号的整数,但%i
inteprets如果前面由输入为十六进制数0x
和八进制如果前面由0
否则解释输入为十进制。
回答by Shafik Yaghmour
There is no difference between the %i
and %d
format specifiers for printf
. We can see this by going to the draft C99 standardsection 7.19.6.1
The fprintf functionwhich also covers printf
with 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 scanf
there is a difference, %d
assume base 10 while %i
auto detects the base. We can see this by going to section 7.19.6.2
The fscanf functionwhich covers scanf
with respect to format specifier, in paragraph 12it says:
另一方面,由于scanf
存在差异,%d
假设基数为 10,而%i
自动检测基数。我们可以通过将部分看到这个7.19.6.2
fscanf函数覆盖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
- 两者是同义词。