In reference to http://cboard.cprogramming.com/showt...232#post841232:
Ok, so here it is: the following C program prints Perl source which, when executed, prints the source of the original C program. q[0] contains header and footer for C, q[1] for Perl respectively.
The function main() simply prints q[1][0] (Perl header), prints q itself in Perl syntax, and q[1][1] (Perl footer). The resulting Perl program does the same analogously.
Note that there is a single blank line at the end of the source. Also note that line endings are '\n', so this might not work properly on native Windows or Mac.Code:#include <stdio.h>
#include <string.h>
static char *q[3][1024] = {
{"\x23\x69\x6e\x63\x6c\x75\x64\x65\x20\x3c\x73\x74\x64\x69\x6f\x2e\x68\x3e\x0a\x23\x69\x6e\x63\x6c\x75\x64\x65\x20\x3c\x73\x74\x72\x69\x6e\x67\x2e\x68\x3e\x0a\x0a\x73\x74\x61\x74\x69\x63\x20\x63\x68\x61\x72\x20\x2a\x71\x5b\x33\x5d\x5b\x31\x30\x32\x34\x5d\x20\x3d\x20\x7b\x0a", "\x09\x7b\x4e\x55\x4c\x4c\x2c\x20\x4e\x55\x4c\x4c\x7d\x0a\x7d\x3b\x0a\x0a\x69\x6e\x74\x20\x6d\x61\x69\x6e\x28\x29\x0a\x7b\x0a\x09\x69\x6e\x74\x20\x69\x2c\x20\x6a\x2c\x20\x6b\x3b\x0a\x0a\x09\x70\x72\x69\x6e\x74\x66\x28\x22\x25\x73\x22\x2c\x20\x71\x5b\x31\x5d\x5b\x30\x5d\x29\x3b\x0a\x09\x66\x6f\x72\x28\x69\x20\x3d\x20\x30\x3b\x20\x71\x5b\x69\x5d\x5b\x30\x5d\x20\x21\x3d\x20\x4e\x55\x4c\x4c\x3b\x20\x69\x2b\x2b\x29\x20\x7b\x0a\x09\x09\x70\x72\x69\x6e\x74\x66\x28\x22\x5c\x74\x5b\x22\x29\x3b\x0a\x09\x09\x66\x6f\x72\x28\x6a\x20\x3d\x20\x30\x3b\x20\x6a\x20\x3c\x20\x32\x3b\x20\x6a\x2b\x2b\x29\x20\x7b\x0a\x09\x09\x09\x70\x72\x69\x6e\x74\x66\x28\x22\x5c\x22\x22\x29\x3b\x0a\x09\x09\x09\x66\x6f\x72\x28\x6b\x20\x3d\x20\x30\x3b\x20\x6b\x20\x3c\x20\x73\x74\x72\x6c\x65\x6e\x28\x71\x5b\x69\x5d\x5b\x6a\x5d\x29\x3b\x20\x6b\x2b\x2b\x29\x20\x7b\x0a\x09\x09\x09\x09\x70\x72\x69\x6e\x74\x66\x28\x22\x5c\x5c\x78\x25\x30\x32\x78\x22\x2c\x20\x71\x5b\x69\x5d\x5b\x6a\x5d\x5b\x6b\x5d\x29\x3b\x0a\x09\x09\x09\x7d\x0a\x09\x09\x09\x69\x66\x28\x6a\x3d\x3d\x30\x29\x20\x70\x72\x69\x6e\x74\x66\x28\x22\x5c\x22\x2c\x20\x22\x29\x3b\x20\x65\x6c\x73\x65\x20\x70\x72\x69\x6e\x74\x66\x28\x22\x5c\x22\x22\x29\x3b\x0a\x09\x09\x7d\x0a\x09\x09\x70\x72\x69\x6e\x74\x66\x28\x22\x5d\x2c\x5c\x6e\x22\x29\x3b\x0a\x09\x7d\x0a\x09\x70\x72\x69\x6e\x74\x66\x28\x22\x25\x73\x22\x2c\x20\x71\x5b\x31\x5d\x5b\x31\x5d\x29\x3b\x0a\x0a\x09\x72\x65\x74\x75\x72\x6e\x20\x30\x3b\x0a\x7d\x0a\x0a"},
{"\x23\x21\x2f\x75\x73\x72\x2f\x62\x69\x6e\x2f\x70\x65\x72\x6c\x0a\x0a\x40\x71\x20\x3d\x20\x28\x0a", "\x09\x5b\x22\x22\x2c\x20\x22\x22\x5d\x0a\x29\x3b\x0a\x0a\x70\x72\x69\x6e\x74\x20\x24\x71\x5b\x30\x5d\x2d\x3e\x5b\x30\x5d\x3b\x0a\x66\x6f\x72\x28\x24\x69\x20\x3d\x20\x30\x3b\x20\x24\x69\x20\x3c\x20\x40\x71\x2d\x31\x3b\x20\x24\x69\x2b\x2b\x29\x20\x7b\x0a\x09\x70\x72\x69\x6e\x74\x28\x22\x5c\x74\x7b\x22\x29\x3b\x0a\x09\x66\x6f\x72\x28\x24\x6a\x20\x3d\x20\x30\x3b\x20\x24\x6a\x20\x3c\x20\x32\x3b\x20\x24\x6a\x2b\x2b\x29\x20\x7b\x0a\x09\x09\x70\x72\x69\x6e\x74\x28\x22\x5c\x22\x22\x29\x3b\x0a\x09\x09\x66\x6f\x72\x65\x61\x63\x68\x20\x24\x63\x20\x28\x73\x70\x6c\x69\x74\x20\x2f\x2f\x2c\x20\x24\x71\x5b\x24\x69\x5d\x2d\x3e\x5b\x24\x6a\x5d\x29\x20\x7b\x0a\x09\x09\x09\x70\x72\x69\x6e\x74\x66\x28\x22\x5c\x5c\x78\x25\x30\x32\x78\x22\x2c\x20\x6f\x72\x64\x28\x24\x63\x29\x29\x3b\x0a\x09\x09\x7d\x0a\x09\x09\x69\x66\x28\x24\x6a\x3d\x3d\x30\x29\x20\x7b\x20\x70\x72\x69\x6e\x74\x28\x22\x5c\x22\x2c\x20\x22\x29\x3b\x20\x7d\x0a\x09\x7d\x0a\x09\x70\x72\x69\x6e\x74\x28\x22\x5c\x22\x7d\x2c\x5c\x6e\x22\x29\x3b\x0a\x7d\x0a\x70\x72\x69\x6e\x74\x20\x24\x71\x5b\x30\x5d\x2d\x3e\x5b\x31\x5d\x3b\x0a\x0a"},
{NULL, NULL}
};
int main()
{
int i, j, k;
printf("%s", q[1][0]);
for(i = 0; q[i][0] != NULL; i++) {
printf("\t[");
for(j = 0; j < 2; j++) {
printf("\"");
for(k = 0; k < strlen(q[i][j]); k++) {
printf("\\x%02x", q[i][j][k]);
}
if(j==0) printf("\", "); else printf("\"");
}
printf("],\n");
}
printf("%s", q[1][1]);
return 0;
}
This concept can easily be extended to increase the number of iterations until the original program appears again, possibly displaying quines in other languages as well.
Quiz question: how do you build a universal Turing machine that prints its own Gödel number?
Greets,
Philip