为什么%在C中打印一半?

Ari*_*n.K 0 c format-specifiers

#include<stdio.h>
main()
{
printf("% % % %");
}
Run Code Online (Sandbox Code Playgroud)

对于上述程序,输出为%%.但为什么?(我用过gcc编译器).

Mik*_*CAT 5

使用无效的格式说明符调用未定义的行为.

N1570 7.21.6.1 fprintf函数在%转换说明符的描述中说:

完整的转换规范应为%%.

N1570 4.符合性说:

如果违反约束或运行时约束之外的''shall''或''shall not''要求,则行为未定义.

%转换说明符的描述在"描述"部分中,而不是约束或运行时约束部分.

在实际实现中,系统的行为如下所示:

  1. 首先读取%并开始解析说明符
  2. 读取 and save it as a flag
  3. Read second %并将其视为%转换说明符
  4.  after that and print it because it is not a part of specifier
  5. Read % %并做1-3工作