Dip*_*ick 265

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

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

所以03327 %i岁,但33岁%d.

  • 在我看来,在sscanf中可能出现零填充的int似乎是最合理的默认行为.如果你不期待Octal,那可能会导致微妙的错误.所以这表明%d是一个很好的说明符,当你必须任意选择一个时,除非你明确想要读取八进制和/或十六进制. (8认同)
  • 啊! 那讲得通!现在我知道要查找的内容了,这也可以在[`printf`](http://www.cplusplus.com/reference/cstdio/printf/)和[`scanf`](http: //www.cplusplus.com/reference/cstdio/scanf/)。 (2认同)

jas*_*son 67

这些是相同的printf但不同的scanf.for printf,both %d%i指定带符号的十进制整数.为scanf,%d并且%i也意味着带符号的整数,但%i如果前面由inteprets输入为十六进制数0x和八进制如果前面由0否则解释输入为十进制.


Sha*_*our 20

%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.
Run Code Online (Sandbox Code Playgroud)

另一方面,由于scanf存在差异,%d假设基数为10而%i自动检测基数.我们可以通过将部分看到这个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.
Run Code Online (Sandbox Code Playgroud)


小智 5

这些话中没有任何一个 - 这两个是同义词.

  • 当用于`scanf()`格式字符串时,有一个区别,如接受的答案所示. (5认同)