转换说明符“%%”应匹配空格

Shall converversion specifier "%%" match white spaces

根据 C 标准,转换说明符 % 定义为:

% Matches a single % character; no conversion or assignment occurs. The complete conversion specification shall be %%.

但是这段代码:

int main(int argc, char* argv[])
{
    int n;
    printf("%d\n", sscanf("     %123", "%% %d", &n));
    return 0;
}

使用 gcc-11.1.0 编译给出输出 1,显然 %% 与字符串的 " %" 匹配。

这似乎违反了“匹配 单个 % 字符”,因为它还接受了 % 字符前面的空格。

问题:根据标准,接受空格作为 %% 指令的一部分是否正确?

根据 C89 标准,至少“输入白色-space 字符 [...] 被跳过,除非规范包含 [cn 说明符。” (这是标准的旧版本,但它是我手头上的那个。但我不认为这在最近的版本中发生了变化。)

我查看了 final C17 draft,实际上有一个具体示例显示 %% 跳过空格:

EXAMPLE 5 The call:

#include <stdio.h>
/* ... */
int n, i;
n = sscanf("foo %bar 42", "foo%%bar%d", &i);

will assign to n the value 1 and to i the value 42 because input white-space characters are skipped for both the % and d conversion specifiers.