我刚刚阅读了
sscanf
手册页,发现了以下内容:
The following conversion specifiers are available:
(...)
d Deprecated. Matches an optionally signed decimal integer; the
next pointer must be a pointer to int.
i Deprecated. Matches an optionally signed integer; the next
pointer must be a pointer to int. The integer is read in base
16 if it begins with 0x or 0X, in base 8 if it begins with 0,
and in base 10 otherwise. Only characters that correspond to
the base are used.
o Deprecated. Matches an unsigned octal integer; the next pointer
must be a pointer to unsigned int.
(...)
为什么
%d
被弃用了?似乎所有 int
说明符均已弃用。
这是什么意思以及有什么可以替代它们?
正如评论中所指出的(感谢@JeffHolt,@Eugene-sh,@DanielWalker,@Barmar,@DanielWalker),答案确实在Bugs部分:
BUGS
Numeric conversion specifiers
Use of the numeric conversion specifiers produces Undefined
Behavior for invalid input. See C11 7.21.6.2/10
⟨https://port70.net/%7Ensz/c/c11/n1570.html#7.21.6.2p10⟩. This is
a bug in the ISO C standard, and not an inherent design issue
with the API. However, current implementations are not safe from
that bug, so it is not recommended to use them. Instead,
programs should use functions such as strtol(3) to parse numeric
input. This manual page deprecates use of the numeric conversion
specifiers until they are fixed by ISO C.
我确实同意“弃用”在这里的意思是“明确反对”(来自@Barmar的评论)。