What should sscanf("x", "%*c%c", &(char){0})
return?
The libraries that Apple and Godbolt are using return −1, which is their EOF
. However, C 2018 7.21.6.7 3 tells us sscanf
returns “EOF
if an input failure occurs before the first conversion (if any) has completed.” %*c
is a conversion specification with assignment suppressed. It completes, and then input failure (end of string) occurs before the %c
completes. Then sscanf
should return “the number of input items assigned,” which is zero.
Is there any text in the standard that would tell us %*c
is to be regarded as not a conversion?
Apparently C99 §7.19.6.2 point 15 (and C17 §7.21.6.2 point 15) tells us that:
Trailing white space (including new-line characters) is left unread unless matched by a directive. The success of literal matches and suppressed assignments is not directly determinable other than via the
%n
directive.
Where "suppressed assignments" is defined as %*
followed by a specifier.
So what you see in your example seems in accordance with the standard: you cannot deduce whether %*c
was matched or not, unless you use a n
specifier to count the number of scanned characters.
Additionally, this comment by user3386109 notes that "the return value is only concerned with the number of assignments" and the C standard text quoted in the question might be intended to say "first assignment" rather than "first conversion".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With