Difference between scanf() and strtol() / strtod() in parsing numbers

后端 未结 8 1594
陌清茗
陌清茗 2020-12-05 20:28

Note: I completely reworked the question to more properly reflect what I am setting the bounty for. Please excuse any inconsistencies with already-given ans

8条回答
  •  南方客
    南方客 (楼主)
    2020-12-05 20:51

    Communication with Fred J. Tydeman, Vice-char of PL22.11 (ANSI "C"), on comp.std.c shed some light on this:

    fscanf

    An input item is defined as the longest sequence of input characters [...] which is, or is a prefix of, a matching input sequence. (7.19.6.2 P9)

    This makes "0x" the longest sequence that is a prefix of a matching input sequence. (Even with %i conversion, as the hex "0x" is a longer sequence than the decimal "0".)

    The first character, if any, after the input item remains unread. (7.19.6.2 P9)

    This makes fscanf read the "z", and put it back as not-matching (honoring the one-character pushback limit of footnote 251)).

    If the input item is not a matching sequence, the execution of the directive fails: this condition is a matching failure. (7.19.6.2 P10)

    This makes "0x" fail to match, i.e. fscanf should assign no value, return zero (if the %x or %i was the first conv. specifier), and leave "z" as the first unread character in the input stream.

    strtol

    The definition of strtol (and strtoul) differs in one crucial point:

    The subject sequence is defined as the longest initial subsequence of the input string, starting with the first non-white-space character, that is of the expected form. (7.20.1.4 P4, emphasis mine)

    Which means that strtol should look for the longest valid sequence, in this case the "0". It should point endptr to the "x", and return zero as result.

提交回复
热议问题