Relatedly: The %x conversion specifier in fscanf is defined in terms of strtoul. fscanf is only allowed one character of lookahead (see §7.23.6.2 footnote 332, “fscanf pushes back at most one input character onto the input stream. Therefore, some sequences that are acceptable to strtod, strtol, etc., are unacceptable to fscanf.”). sscanf and scanf are both defined in terms of fscanf.
Consider this sequence of code:
unsigned int o; int n; int r = sscanf("0xZZ", "%x%n", &o, &n);Is the result
- r=1 (one conversion was possible), n=1 (one byte of input consumed), o=0 (we consumed the single zero digit)
- r=0 (no conversion was possible) because 0x is a valid prefix of a conversion specifier but is not a valid input to strtoul (see previous post)
Now consider
int r = sscanf("0xAA", "%x%n", &o, &n);We have a third potential option here: r=1, n=4, o=0xAA
however per the above footnote we must pick that our result is
- (1) in both cases
- (2) in both cases
- (2) in the first case, (3) in the second
In particular combo (1) and (3) is impossible because it would require us to peak ahead two characters (xZ/xA) in order to know which of the two cases we are in
I think (1) in both cases is the only reasonable result and the standard should probably call out explicitly that the 0b/0x prefixes are not permitted for the input to the %b/%x conversion specifiers respectively.