打印自己的程序,它是如何工作的?

Spe*_*ine 18 c quine

我遇到了一个在这个网站上打印自己的程序,即打印程序代码.

程序代码是:

#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格式说明符允许打印出特殊字符,如换行符,否则无法嵌入到输出字符串.

我不明白该程序是如何工作的.我已经提到了我需要解释的线条,它们如何工作以及它们在做什么.请解释.

amm*_*r26 8

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参数正在执行的

  • 程序,传递要打印的字符串
  • 10,为第一个%c传递10个ASCII码(将转换为字符换行符)
  • 程序,再次将相同的字符串传递给程序中的%s再次打印相同的字符串
  • 34,传递34个ASCII码为第二个%c(将被转换为字符双qoutes)
  • 10,传递10个ASCII码为3%c(将转换为字符换行符)
  • 10,传递10个ASCII码为4%c(将转换为字符换行符)
  • 10,传递10个ASCII码为5%c(将转换为字符换行符)
  • 10,传递10个ASCII码为6%c(将转换为字符换行符)
  • 10,传递10个ASCII代码为7th%c(将转换为字符换行符)
  • 10,传递10个ASCII码为8%c(将转换为字符换行符)