是什么转换标识符%I和%d在格式化IO的功能(* printf的/ * scanf的)之间的差
-
19-09-2019 - |
题
如在
%d
格式说明中使用时是什么%i
和printf
之间的差异?
解决方案
它们是同用于输出时,例如与printf
。
但是,这些作为输入符例如使用时是不同的与scanf
,其中%d
扫描一个整数作为符号十进制数,但%i
默认为十进制但也允许十六进制(如果前面由0x
)和八进制(如果前面由0
)。
因此033
将27 %i
但33 %d
。
其他提示
这些是相同的为printf
但scanf
不同。对于printf
,既%d
和%i
指定签署十进制整数。对于scanf
,%d
和%i
也意味着有符号整数,但如果前面由%i
和八进制如果前面由0x
0
inteprets输入为十六进制数和以其他方式解释输入为十进制。
有是%i
和%d
格式说明printf
之间没有差别。我们可以通过转到草案C99标准看到这一>部分7.19.6.1
fprintf函数又包括printf
相对于格式说明,它说在段落 8 :
转换标识符和它们的含义如下:
和包括下列子弹:
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.
在另一方面,对于scanf
是有区别的,而%d
自动检测基%i
假定基座10。我们可以通过将部分7.19.6.2
看到的 fscanf函数的覆盖scanf
关于格式说明,段的 12 的它说:
转换标识符和它们的含义如下:
和包括以下内容:
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.
这里没有任何在printf
- 两个是同义词
不隶属于 StackOverflow