Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is an fscanf/scanf/sscanf conversion specification with assignment suppressed not a conversion?

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?

like image 635
Eric Postpischil Avatar asked Oct 15 '22 20:10

Eric Postpischil


1 Answers

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".

like image 183
Marco Bonelli Avatar answered Oct 18 '22 22:10

Marco Bonelli