diff options
| author | Ethan Werner <mail@ethanwerner.com> | 2026-10-10 20:55:33 +0000 |
|---|---|---|
| committer | Ethan Werner <mail@ethanwerner.com> | 2026-10-10 20:56:50 +0000 |
| commit | f8423ebea413605b42de23f5dd06c5479d3fa1dd (patch) | |
| tree | 2612a5eb026c59e7aae01f10a1cf62f2ee43042c /tests/test_quine.c | |
Diffstat (limited to 'tests/test_quine.c')
| -rw-r--r-- | tests/test_quine.c | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/tests/test_quine.c b/tests/test_quine.c new file mode 100644 index 0000000..c010539 --- /dev/null +++ b/tests/test_quine.c @@ -0,0 +1,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; +} |