我遇到了一个在这个网站上打印自己的程序,即打印程序代码.
程序代码是:
#include <stdio.h>
char *program = "#include <stdio.h>%cchar *program = %c%s%c;%cint main()%c{%cprintf(program, 10, 34, program, 34, 10, 10, 10, 10, 10, 10);%c return 0;%c}%c";
//what is this line doing, what is the use of %c and %s and what properties of %c and %s are being used here?
int main()
{
printf(program, 10, 34, program, 34, 10, 10, 10, 10, 10, 10);
//what is this print function doing, and how?
return 0;
}
Run Code Online (Sandbox Code Playgroud)
并给出的解释是:
这里的两个关键技巧是使用带有嵌入式%s说明符的字符串,以允许字符串在打印时包含自身,并使用%c格式说明符允许打印出特殊字符,如换行符,否则无法嵌入到输出字符串.
我不明白该程序是如何工作的.我已经提到了我需要解释的线条,它们如何工作以及它们在做什么.请解释.
char *program = "#include <stdio.h>%cchar *program = %c%s%c;%cint main()%c{%cprintf(program, 10, 34, program, 34, 10, 10, 10, 10, 10, 10);%c return 0;%c}%c";
Run Code Online (Sandbox Code Playgroud)
有一个char指针名称"program",用于存储字符串,%c和%s分别是char和string参数的格式说明符.
printf(program, 10, 34, program, 34, 10, 10, 10, 10, 10, 10);
Run Code Online (Sandbox Code Playgroud)
printf函数是打印输出到控制台,10这里是NEWLINE的ASCII代码,34是"printf参数正在执行的