c - 为什么 Ken Thompson 在 "Reflections on Trusting Trust"中提到的 C quine 不起作用?

标签 c quine

我在 Ken Thompson 的一篇文章(阅读 here)中看到的这个 quine 没有重现相同的代码。我只是好奇为什么它不起作用?代码现在过时了吗?

quine代码:

char s[] = {
        '\t',
        '0',
        '\n',
        '}',
        ';',
        '\n',
        '\n',
        '/',
        '*',
        '\n'
};

/*
 *The string s is a representation of the body
 *of this program from '0'
 * to the end
 */

main(){
        int i;

        printf("char\ts[] = {\n");
        for(i = 0; s[i]; i++)
                printf("\t%d, \n", s[i]);
        printf("%s",s);
}

输出:

char    s[] = {
        9,
        48,
        10,
        125,
        59,
        10,
        10,
        47,
        42,
        10,
        0
};

/*

这些是编译时的编译器警告(self_reproducing.c 是文件名):

self_reproducing.c:20:1: warning: return type defaults to ‘int’ [-Wimplicit-int]
   20 | main(){
      | ^~~~
self_reproducing.c: In function ‘main’:
self_reproducing.c:23:2: warning: implicit declaration of function ‘printf’ [-Wimplicit-function-declaration]
   23 |  printf("char\ts[] = {\n");
      |  ^~~~~~
self_reproducing.c:23:2: warning: incompatible implicit declaration of built-in function ‘printf’
self_reproducing.c:1:1: note: include ‘<stdio.h>’ or provide a declaration of ‘printf’
  +++ |+#include <stdio.h>
    1 | char s[] = {

糟糕!我忽略了 213 lines deleted 行。所以问题应该是——文章中提到的整个 quine 是什么?

最佳答案

我认为“不工作”意味着不“给出编译器警告”——因为这些原因是不言而喻的——而是你写的其他内容:

This quine [...] isn't reproducing the same code

quine self 复制的,一旦我们添加回 (删除 213 行),作者删节了而你删除了 - 这代表了代码的其余部分,加上结束 chars 数组的 0 NUL 终止符:

/*
 * The string s is a representation of the body
 * of this program from '0'
 * to the end
 */

您的困惑似乎源于程序如何打印所述数组中字符的整数 ASCII 值,但它最初声明这些字符如 '\t''\n' ,等等。但是具有适当值的整数是有效的char,因此代码 self 复制的,即使元素被格式化为整数而不是'c'characters 与原始来源一样。无论哪种方式,我们最终都会得到一个包含相同 char 的数组;只是该数组以不同但等效的方式初始化。

为什么将它们打印为整数?因为这比必须为每个元素引用、有条件地转义等更容易/更短。它映射到相同的机器代码/数组内容,但需要更少的 faff 来对 quine 进行编程。

作者竟然写了这个!

(The purist will note that the program is not precisely a self-reproducing program, but wIll produce a self-reproducing program.)

至于编辑/添加的问题,整个quine当然会添加代表程序其余部分所需的其余字符。作者大概是为了能够更轻松地说明他们的观点而删减了该程序,而无需通过占用整页来将其带回家。

关于c - 为什么 Ken Thompson 在 "Reflections on Trusting Trust"中提到的 C quine 不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62679621/

相关文章:

java - 除了维基百科页面上列出的那个,还有其他 Java quines 吗?

从 Linux 到 Windows 7 上的 Visual Studio 2010 的 C 编程问题

c++ - 超线程 - 我可以通过哪个测试来检查它是启用还是禁用?

c - 这个程序如何 self 复制?

r - function(){} 是真正的奎因吗?

java - 加号运算符 - 如何强制执行字符串连接?

c - 运算符 [] 的正确输入数据类型是什么?

c - 将链表节点插入升序单链表GCC错误: dereferencing pointer to incomplete type

c - 是否有 fsync 但带有路径参数