summaryrefslogtreecommitdiffstats
path: root/tests/test_quine.c
blob: c0105397f9fe416f3cd8f6276ec4b15203c9d559 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/* test_quine.c — a C quine: running it prints its own source.
 *
 * How it works: the file is a fixed point of a "quinate" function.
 * The two strings below are that function's data; everything around
 * them is code that re-emits the data verbatim, spliced back into the
 * same program shape.  No argv, no /proc/self, no tricks — just two
 * printf calls.
 *
 * Build:  musl-gcc -std=gnu11 -Oz -Wall -Wextra -o test_quine tests/test_quine.c
 * Check:  ./test_quine | diff - tests/test_quine.c   (no output = quine) */

#include <stdio.h>

int main(void) {
    const char *s =
        "const char *s = \"%s\";\n"
        "const char *f = \"%s\";\n"
        "printf(f, s, s, f);\n"
        "return 0;\n";
    const char *f =
        "int main(void) {\n"
        "    %s    printf(f, s, s, f);\n"
        "    return 0;\n"
        "}\n";
    printf(f, s, s, f);
    return 0;
}