Search code examples
cscanfgnuglibc

man sscanf: %d is deprecated in C or glibc?


I was just reading the glibc sscanf man page (from the Linux man-pages package) and I found the following:

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.

(...)

  • How come %d is deprecated? It seem that all int specifiers are deprecated.
  • What does it mean and what is there to replace them?

Solution

  • As pointed in the comments (thanks to @JeffHolt, @Eugene-sh, @DanielWalker, @Barmar, @DanielWalker) , the answer is indeed in the Bugs section:

    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.
    

    I do agree that of "deprecate" means here "express disapproval of" (as from @Barmar's comment).