Consider the following scanf
conversion specifiers and inputs:
"%4c"
and "abc"
"%x"
and "0x"
"%f"
and "1.0e+"
That is, cases where the input is an initial subsequence of a match, but not a match. Assuming EOF is reached after the incomplete match, is scanf
supposed to return EOF
or 0
? The text in C99 reads:
The fscanf function returns the value of the macro EOF if an input failure occurs before any conversion. Otherwise, the function returns the number of input items assigned, which can be fewer than provided for, or even zero, in the event of an early matching failure.
And in POSIX 2008, it reads:
Upon successful completion, these functions shall return the number of successfully matched and assigned input items; this number can be zero in the event of an early matching failure. If the input ends before the first matching failure or conversion, EOF shall be returned. If any error occurs, EOF shall be returned, [CX] and errno shall be set to indicate the error. If a read error occurs, the error indicator for the stream shall be set.
What's unclear to me is whether the partial but incomplete match constitutes an "early matching failure". I would find the return value of 0 in this case a lot more useful (it distinguishes the cases of plain EOF versus invalid truncated data) but what I'm looking for is just help interpreting the standard.
Note that glibc's scanf
is completely incorrect on all of these inputs and returns 1
, treating the invalid input as a match. I'm pretty sure this issue has been reported and marked WONTFIX. :-(
I'm expecting 0.
7.16.6.2/4:
Failures are described as input failures (due to the occurence of an encoding error or the unavailability of inout characters), or matching failures (due to inappropriate input).