【发布时间】:2011-10-07 17:39:58
【问题描述】:
我遇到了一个在this 网站上打印自己的程序,即打印程序代码。
程序代码为:
#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 格式说明符来允许打印出特殊字符 像换行符,否则不能嵌入到输出中 字符串。
我不明白程序是如何工作的。我已经提到了我需要解释的行,它们是如何工作的以及它们在做什么。请解释一下。
【问题讨论】:
-
我最近多次看到这个问题。这是家庭作业还是程序员挑战?
-
没有这样的,我只是偶然发现的。如果是作业,我会使用作业标签。
-
您的 cmets 打破了 quine :) 此外,quine 中的缩进也不正确。 this is my most recent quine in c++@MichaelDorgan: 是的,这是一个有历史根源的程序员挑战
-
我更喜欢这个版本:
main(){char*s="main(){char*s=%c%s%c;printf(s,34,s,34);}";printf(s,34,s,34);}(末尾没有换行符)。