当用作printf
中的格式说明符时,%d
和%i
之间有什么区别?格式化IO函数(* printf/* scanf)中的转换说明符%i和%d之间有什么区别
155
A
回答
216
它们在用于输出时是相同的,例如,与printf
。
但是,当用作输入说明符时,这些是不同的。与scanf
,其中%d
扫描一个整数作为带符号的十进制数,但%i
默认为十进制,但也允许十六进制(如果前面有0x
)和八进制(如果前面跟着0
)。
因此033
将27与%i
但33与%d
。
9
这些词中没有任何 - 这两个词是同义词。
+0
在接受的答案中提到,在scanf()格式的字符串中使用时有区别。 –
62
这些对于printf
是相同的,但对于scanf
是不同的。对于printf
,%d
和%i
均指定一个带符号的十进制整数。对于scanf
,%d
和%i
也表示有符号整数,但%i
将输入解释为十六进制数字,前面为0x
,而八进制为前面的0
,否则将输入解释为十进制。
14
对于printf
,%i
和%d
格式说明符之间没有区别。我们可以通过转到draft C99 standard部分7.19.6.1
fprintf函数又包括printf
关于格式说明看到这一点,它在一段说:
转换标识符和它们的含义如下:
,并包括以下子弹:
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
自动检测基数。我们可以通过将部分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.
相关问题
- 1. printf(“%d”,i ++)和i ++之间有什么区别;的printf( “%d”,i)的?
- 2. C中的%i,%d和%D有什么区别? (printf)
- 3. 作为scanf的格式说明符,%* c%c和%c之间有什么区别?
- 4. %s和%c格式说明符之间有什么区别?
- 5. Java:格式说明符%x和%h之间有什么区别?
- 6. scanf(“%d”,* p)和scanf(“%d”,p)有什么区别?
- 7. 解释scanf中没有空格和scanf中空白之间有什么区别?
- 8. getch()和scanf()函数之间的区别
- 9. Python字符串格式化中%s和%d有什么区别?
- 10. %ul和%lu C格式说明符有什么区别?
- 11. 什么的printf( “%I”)和printf( “%d”)
- 12. i-vector和d-vector之间的区别
- 13. 什么是scanf(“%* s”)和scanf(“%* d”)格式标识符?
- 14. 说明铸造和转换 - 简单的词有什么区别
- 15. 格式说明符之后的scanf空格与格式说明符之前的空格(“%c”vs“%c”)
- 16. 是什么的printf( “\%d”,&t);和printf( “%d”,T)之间的差;
- 17. 访问修饰符和访问说明符之间有什么区别
- 18. C中限定符和说明符之间的区别是什么?
- 19. Typedefs和printf格式说明符
- 20. printf的格式说明符中的'F'是什么意思?
- 21. fork()和〜fork()之间有什么区别
- 22. “%。* s”是什么意思作为printf中的格式说明符?
- 23. scanf说明符“n”是什么数字?
- 24. 为什么printf(“%d%d%d”,++ i,i,i ++)是未定义的行为?
- 25. C “%d” 格式说明
- 26. 很少有新手的问题,与scanf函数和转换说明问题
- 27. 为什么我应该用“%d”代替“%i”作为C中整数的转换说明符?
- 28. * x = i和x =&i之间的区别是什么
- 29. 不同格式说明符的scanf函数的不同行为
- 30. 中有什么函数参数和参数之间......区别
在sscanf中期待一个int可能的零填充在我看来是最合理的默认行为。如果你不期待Octal,那可能会导致微妙的错误。所以这表明当你必须任意选择一个时,%d是一个很好的说明符,除非你明确地想要读八进制和/或十六进制。 – Eliot