打印自身的程序,它是如何工作的?
我在此网站上发现了一个可以打印自身的程序,即它打印程序代码。
程序代码为:
#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;
}
给出的解释为:
这里的两个关键技巧是使用嵌入 %s 的字符串 说明符允许字符串在打印时包含其自身,并 使用 %c 格式说明符允许打印特殊字符 就像换行符一样,否则无法将其嵌入到输出中 字符串。
我不明白该程序是如何工作的。我已经提到了我需要解释的行,它们是如何工作的以及它们在做什么。请解释一下。
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.
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
有一个名为“program”的字符指针,用于存储字符串,%c 和 %s 分别是字符和字符串参数的格式说明符。
printf 函数将输出打印到控制台,这里 10 是换行符的 ASCII 代码,34 是“
printf 参数正在执行
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.
printf function is printing output to console, 10 here is ASCII code for NEWLINE and 34 for "
printf parameters are doing
Printf 打印作为第一个参数给出的字符串(在本例中为
*program
中的字符串),替换具有 %s 或 %c 的其他参数%s 表示参数是一个字符串,%c是一个字符。
正如注释所说,它使用 %s 打印程序字符串内的程序字符串的副本 - 因此制作副本,并使用 %c 打印字符 10 (换行)和 34
"
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%s means the arguement is a string, %c is a character.
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
"
为了更好地理解,变量
program
可以这样写:这个想法是,你运行程序,编译它的输出,运行那个程序等等。但这只能通过 %c 值 10(用于换行)和 34(用于双引号)来完成。
For a better understanding, the variable
program
could have been written like this: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.
这可以使用文件处理来完成。使用任意名称保存程序并将该名称放入 fopen 命令中打开的目录中。
比如我的程序的名字是hello.cpp。
这是下面的程序
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.
This is the following program