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

时间:2021-11-26 16:58:49

I came across a program that prints itself on this site, i.e. it prints the program code.

我遇到了一个在这个网站上打印自己的程序,也就是说,它打印程序代码。

The program code is:

程序代码是:

#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;
}

And the explanation given is:

给出的解释是:

The two key tricks here are using a string with an embedded %s specifier to allow the string to contain itself when printed, and to use the %c format specifier to allow printing out special characters like newlines, which could not otherwise be embedded in the output string.

这里的两个关键技巧是使用一个带有嵌入式%s说明符的字符串来允许字符串在打印时包含自己,并使用%c格式说明符来打印特殊字符,如换行符,否则不能将其嵌入到输出字符串中。

I didn't understand how the program is working. I have mentioned the lines i need the explanation about, how they work and what are they doing. Please explain.

我不明白这个程序是如何工作的。我已经提到了我需要解释的线,它们如何工作,它们在做什么。请解释一下。

4 个解决方案

#1


7  

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";

There is a char pointer name "program" which is used to store the string and %c and %s are format specifiers for char and string arguments respectively.

有一个char指针名称“程序”,用于存储字符串,%c和%s分别是char和string参数的格式说明符。

printf(program, 10, 34, program, 34, 10, 10, 10, 10, 10, 10);

printf function is printing output to console, 10 here is ASCII code for NEWLINE and 34 for " printf parameters are doing

printf函数是打印输出到控制台,这里10是为NEWLINE的ASCII码,34为“printf参数正在做的”。

  • program , passing string to be printed
  • 程序,传递要打印的字符串
  • 10 , passing 10 ASCII code for first %c (will be converted to character newline)
  • 10,为第一个%c传递10个ASCII码(将转换为字符换行)
  • program , passing same string again to %s in program to print same string again
  • 程序,在程序中再次将相同的字符串传递给%s,以便再次打印相同的字符串
  • 34 , passing 34 ASCII code for second %c (will be converted to character double qoutes)
  • 34,将34个ASCII码转换为第二个%c(将转换为字符double qoutes)
  • 10 , passing 10 ASCII code for 3rd %c (will be converted to character newline)
  • 10、为第三%c传递10个ASCII码(将转换为换行字符)
  • 10 , passing 10 ASCII code for 4th %c (will be converted to character newline)
  • 10、为4% c传递10个ASCII码(将转换为换行字符)
  • 10 , passing 10 ASCII code for 5th %c (will be converted to character newline)
  • 10、为第5 %c传递10个ASCII码(将转换为换行字符)
  • 10 , passing 10 ASCII code for 6th %c (will be converted to character newline)
  • 10、为第6 %c传递10个ASCII码(将转换为换行字符)
  • 10 , passing 10 ASCII code for 7th %c (will be converted to character newline)
  • 10、为7% c传递10个ASCII码(将转换为换行字符)
  • 10 , passing 10 ASCII code for 8th %c (will be converted to character newline)
  • 10、传递8 %c的10个ASCII码(将转换为换行字符)

#2


2  

Printf prints the string given as the first argument (in this case the string in *program) substituting the other arguments where you have a %s or %c

Printf打印作为第一个参数的字符串(在本例中是*程序中的字符串),替换具有%s或%c的其他参数

%s means the arguement is a string, %c is a character.

%s表示论述是字符串,%c是字符。

As the note says, it uses %s to print a copy of the program string inside the program string - hence making a copy, and uses the %c to print the characters 10 (new line) and 34 "

如注释所述,它使用%s在程序字符串中打印程序字符串的副本—因此进行复制,并使用%c打印字符10(新行)和34”

#3


0  

For a better understanding, the variable program could have been written like this:

为了更好地理解,变量程序可以这样写:

"#include <stdio.h>\nchar *program = \"%s\";\nint main()\n..."

The idea is, that you run the program, compile it's output, run that program and so on. But this can only been done with %c values 10 for linefeed and 34 for double quote.

这个想法是,你运行程序,编译它的输出,运行那个程序等等。但是这只能在%c值为10的linefeed和34的双引号中完成。

#4


-1  

This can be done using File handling. Save the program with any name and put that name in the open directory in fopen command. Like my program's name is hello.cpp.

这可以通过文件处理来实现。用任何名称保存程序,并将该名称放在fopen命令的open目录中。就像我的程序的名字是hello.cpp。

This is the following program

这是下面的程序

#include <stdio.h>
#include <iostream>
int main()
{
    FILE *fp;   
    fp=fopen("hello.cpp","r");
    char ch;
    while((ch=fgetc(fp))!=EOF)
    {
       printf("%c",ch);
     }
}

#1


7  

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";

There is a char pointer name "program" which is used to store the string and %c and %s are format specifiers for char and string arguments respectively.

有一个char指针名称“程序”,用于存储字符串,%c和%s分别是char和string参数的格式说明符。

printf(program, 10, 34, program, 34, 10, 10, 10, 10, 10, 10);

printf function is printing output to console, 10 here is ASCII code for NEWLINE and 34 for " printf parameters are doing

printf函数是打印输出到控制台,这里10是为NEWLINE的ASCII码,34为“printf参数正在做的”。

  • program , passing string to be printed
  • 程序,传递要打印的字符串
  • 10 , passing 10 ASCII code for first %c (will be converted to character newline)
  • 10,为第一个%c传递10个ASCII码(将转换为字符换行)
  • program , passing same string again to %s in program to print same string again
  • 程序,在程序中再次将相同的字符串传递给%s,以便再次打印相同的字符串
  • 34 , passing 34 ASCII code for second %c (will be converted to character double qoutes)
  • 34,将34个ASCII码转换为第二个%c(将转换为字符double qoutes)
  • 10 , passing 10 ASCII code for 3rd %c (will be converted to character newline)
  • 10、为第三%c传递10个ASCII码(将转换为换行字符)
  • 10 , passing 10 ASCII code for 4th %c (will be converted to character newline)
  • 10、为4% c传递10个ASCII码(将转换为换行字符)
  • 10 , passing 10 ASCII code for 5th %c (will be converted to character newline)
  • 10、为第5 %c传递10个ASCII码(将转换为换行字符)
  • 10 , passing 10 ASCII code for 6th %c (will be converted to character newline)
  • 10、为第6 %c传递10个ASCII码(将转换为换行字符)
  • 10 , passing 10 ASCII code for 7th %c (will be converted to character newline)
  • 10、为7% c传递10个ASCII码(将转换为换行字符)
  • 10 , passing 10 ASCII code for 8th %c (will be converted to character newline)
  • 10、传递8 %c的10个ASCII码(将转换为换行字符)

#2


2  

Printf prints the string given as the first argument (in this case the string in *program) substituting the other arguments where you have a %s or %c

Printf打印作为第一个参数的字符串(在本例中是*程序中的字符串),替换具有%s或%c的其他参数

%s means the arguement is a string, %c is a character.

%s表示论述是字符串,%c是字符。

As the note says, it uses %s to print a copy of the program string inside the program string - hence making a copy, and uses the %c to print the characters 10 (new line) and 34 "

如注释所述,它使用%s在程序字符串中打印程序字符串的副本—因此进行复制,并使用%c打印字符10(新行)和34”

#3


0  

For a better understanding, the variable program could have been written like this:

为了更好地理解,变量程序可以这样写:

"#include <stdio.h>\nchar *program = \"%s\";\nint main()\n..."

The idea is, that you run the program, compile it's output, run that program and so on. But this can only been done with %c values 10 for linefeed and 34 for double quote.

这个想法是,你运行程序,编译它的输出,运行那个程序等等。但是这只能在%c值为10的linefeed和34的双引号中完成。

#4


-1  

This can be done using File handling. Save the program with any name and put that name in the open directory in fopen command. Like my program's name is hello.cpp.

这可以通过文件处理来实现。用任何名称保存程序,并将该名称放在fopen命令的open目录中。就像我的程序的名字是hello.cpp。

This is the following program

这是下面的程序

#include <stdio.h>
#include <iostream>
int main()
{
    FILE *fp;   
    fp=fopen("hello.cpp","r");
    char ch;
    while((ch=fgetc(fp))!=EOF)
    {
       printf("%c",ch);
     }
}