According to the specification of fscanf
in C17 7.21.6.2/8:
Input white-space characters (as specified by the
isspace
function) are skipped, unless the specification includes a[
,c
, orn
specifier
If the format string contains %%
, then it is a specification with a %
specifier. That isn't [
, c
, or n
, so the standard appears to say that leading whitespace should be skipped here.
My question is: is this a correct interpretation, or is it a defect in the standard?
I tested two different implementations (mingw-w64 with MSVCRT stdio, and mingw-w64 with MinGW stdio). The former did not skip leading whitespace, the latter did.
Test code:
#include <stdio.h>
int main(void)
{
int a, r;
// Should be 1 according to standard; would be 0 if %% does not skip whitespace
r = sscanf("x %1", "x%% %d", &a);
printf("%d\n", r);
// Should always be 1
r = sscanf("x%1", "x%% %d", &a);
printf("%d\n", r);
}