Answers:
它们用于输出时是相同的,例如与printf
。
但是,当用作输入说明符时,它们是不同的,例如与一起使用scanf
,其中%d
扫描一个整数作为有符号的十进制数字,但%i
默认为十进制,但也允许十六进制(如果以开头0x
)和八进制(如果以开头0
)。
所以033
27将与%i
33一起%d
。
的%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
假设基数为10,而%i
自动检测基数。我们可以从第12段7.19.6.2
的fscanf函数部分(涉及scanf
格式说明符)中看到这一点:
转换说明符及其含义是:
并包括以下内容:
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.