一个复制自己的程序 [英] A program that reproduces itself

查看:61
本文介绍了一个复制自己的程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是一个C程序,取自
http:/ /en.wikipedia.org/wiki/Quine#Sample_quine_in_C

具有好奇的属性,在执行时,它会生成自己的

源代码为输出。


#include< stdio.h>

char x [] =" #include< stdio.h>%cchar x [] =%c%s%c;%cint main()

{printf(x,10,34,x,34,10,10);返回0;}%c" ;;

int main(){printf(x,10,34,x,34,10,10);返回0;}


但我无法想象出来!具体来说,我不知道

硬编码整数的重要性,也不知道这个

实例中%格式化字符是如何工作的。有人可以解释一下这是如何工作的吗?

解决方案



" Robert Rotstein" < RR ******* @ verizon.net>在消息中写道

news:YUecd.1533


B34.281@trndny02 ...

以下是C程序,已采取来自
http://en.wikipedia.org/wiki/Quine #Sample_quine_in_C
具有好奇的属性,在执行时会产生自己的源代码作为输出。

#include< stdio.h>
char x [] =" #include< stdio.h>%cchar x [] =%c%s%c;%cint main()
{printf(x,10,34, x,34,10,10); return 0;}%c" ;;
int main(){printf(x,10,34,x,34,10,10); return 0;}

但我无法理解!具体来说,我不知道
硬编码整数的重要性,


它们是字符编码(假设ASCII字符集,

使程序不可移植)。 10是换行符,

34是双引号字符()(对于ASCII)。

也不是%格式化字符如何工作在这个
实例。


他们按照''printf()''函数的规范

定义的''正常'方式工作。 %c格式化一个字符输出,

%s格式化一个字符串(char *)输出。

有人可以解释它是如何工作的吗?




数组''x''是''printf()''的第一个(''format'')参数

和10,34,x,34 ,10,10是随后的''printf()''参数

,其输出格式由''''中的%说明符指定。


如果你改变

''printf()''到''sprintf()''(其第一个参数是一个指针
$),它可能会帮助你看看发生了什么。 b $ b到一个字符串,其中输出将被写入(而不是

''stdout''所有其他参数都是相同的。)跟随

''puts()''表示该字符串。注意你需要

为''sprintf()''提供数组写入。


HTH,

-Mike


-Mike




2004年10月16日星期六,Robert Rotstein写道:


以下是一个C程序,取自
http://en.wikipedia.org/wiki/Quine#Sample_quine_in_C ,具有好奇的属性,在执行时,它会生成自己的源代码作为输出。


GoogleC quine许多,更多。

#include< stdio.h>
char x [] =" #include< stdio.h>%cchar x [] =%c% s%c;%cint main()
{printf(x,10,34,x,34,10,10); return 0;}%c" ;;
int main(){printf( x,10,34,x,34,10,10);返回0;}

但我无法理解!具体来说,我不知道
硬编码整数的重要性,


它们是字符''\\ n'的ASCII编码值''(10)和''"''

(34)。程序员依赖于这样的假设:这个程序只会在使用ASCII字符编码的实现上运行,而b / b $ b ...这通常是一个合理的假设 - - 意味着该程序

不是严格符合C程序。

(字符串文字也有一个嵌入式换行符,绝对是

不是有效的C,但那只是你发布技术的一个神器;维基百科上的原始程序没有那个缺陷。)

也不是%格式化字符在此
实例中有效。


他们总是这样做:%s打印一个字符串,%c打印一个字符。

你不了解他们吗?

有人可以解释一下这是如何工作的吗?




只需运行它,知道程序员写的时候

10,他预计会在输出中看到一个换行符,当他写下

34时,他预计会看到双引号。


-Arthur


Blatant plug: http://www.contrib.andrew.cmu.edu/~a...ftware/quine.c

Following is a C program, taken from
http://en.wikipedia.org/wiki/Quine#Sample_quine_in_C,
which has the curious property that, when executed, it produces its own
source code as output.

#include <stdio.h>
char x[]="#include <stdio.h>%cchar x[]=%c%s%c;%cint main()
{printf(x,10,34,x,34,10,10);return 0;}%c";
int main() {printf(x,10,34,x,34,10,10);return 0;}

But I can''t figure it out! Specifically, I don''t know the significance of
the hard-coded integers, nor how the % formatting characters work in this
instance. Can someone explain just how this works?

解决方案


"Robert Rotstein" <rr*******@verizon.net> wrote in message
news:YUecd.1533


B34.281@trndny02...

Following is a C program, taken from
http://en.wikipedia.org/wiki/Quine#Sample_quine_in_C,
which has the curious property that, when executed, it produces its own
source code as output.

#include <stdio.h>
char x[]="#include <stdio.h>%cchar x[]=%c%s%c;%cint main()
{printf(x,10,34,x,34,10,10);return 0;}%c";
int main() {printf(x,10,34,x,34,10,10);return 0;}

But I can''t figure it out! Specifically, I don''t know the significance of
the hard-coded integers,
They are character encodings (which assume the ASCII character set,
which makes the program nonportable). 10 is the newline character,
34 is the double-quote character(") (for ASCII).
nor how the % formatting characters work in this
instance.
They work in the ''normal'' way defined by the specification
of the ''printf()'' function. %c formats a character output,
%s formats a string (char*) output.
Can someone explain just how this works?



The array ''x'' is the first (''format'') argument to ''printf()''
and 10, 34, x, 34, 10, 10 are the subsequent ''printf()'' arguments
whose output formatting is specified by the % specifiers in ''x''.

It might help you see what''s happening if you change the
''printf()'' to ''sprintf()'' (whose first argument is a pointer
to a string where the output will be written (instead of to
''stdout'' All the other parameters are the same). Follow that
with a ''puts()'' of that string. Note that you''ll need to
provide the array for ''sprintf()'' to write to.

HTH,
-Mike

-Mike



On Sat, 16 Oct 2004, Robert Rotstein wrote:


Following is a C program, taken from
http://en.wikipedia.org/wiki/Quine#Sample_quine_in_C,
which has the curious property that, when executed, it produces its own
source code as output.
Google "C quine" for many, many more.
#include <stdio.h>
char x[]="#include <stdio.h>%cchar x[]=%c%s%c;%cint main()
{printf(x,10,34,x,34,10,10);return 0;}%c";
int main() {printf(x,10,34,x,34,10,10);return 0;}

But I can''t figure it out! Specifically, I don''t know the significance of
the hard-coded integers,
They''re the ASCII-encoded values of the characters ''\n'' (10) and ''"''
(34). The programmer is relying on the assumption that this program will
only ever be run on implementations using the ASCII character encoding,
which---while generally a reasonable assumption---means that the program
is not a strictly conforming C program.
(The string literal also has an embedded newline, which is definitely
not valid C, but that''s just an artifact of your posting technique; the
original program on Wikipedia doesn''t have that defect.)
nor how the % formatting characters work in this
instance.
Same way they always do: %s prints a string, %c prints a character.
What don''t you understand about them?
Can someone explain just how this works?



Just run through it, with the knowledge that when the programmer writes
"10", he''s expecting to see a newline in the output, and when he writes
"34", he''s expecting to see a double quote.

-Arthur

Blatant plug: http://www.contrib.andrew.cmu.edu/~a...ftware/quine.c


这篇关于一个复制自己的程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆