tech-userlevel archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: Bug in scanf?



On Wed, Dec 10, 2025 at 03:55:22PM +0100, Edgar Fuß wrote:
> I still don't quite get this.
> 
> As far as I understand, scanning stops at the "r" and the "e" is pushed back.
> I fail to see where the standard commands that the number before the "ergs" 
> is discarded.
> What would people expect from strtol("100foo", &p, 10)?
> I expect 100 to be returned and p to point to "foo", which is what happens.

The problem is that the syntax given for %f input makes 'e'{digit}+
optional, but only together and the parser can only decide it is there
or not when seeing the one char lookahead "e" input. So it decides the
exponent expression is there and then fails to parse it (no digits
following), which means scanf aborts due to a match failure and
returns the number of expressions parsed so far (0 in this example).

With two chars lookahead the parser can realize the e is not start of an
exponent expression and ignore it totally. So can strtod() which only needs
to keep additional state by a simple count (or pointer) pointing at the
last properly consumed input character.

Martin


Home | Main Index | Thread Index | Old Index