Maxs homepage

Main Branches

Contact   Publications   Talks   Grants   Teaching   Mathematics   Computer   Images   Private   Links  

A C program, that prints itself (1)

#include <stdio.h>
char buffer[80];
void quote(char *p) {
  char *q = buffer;
  while (*p) {
    if (*p == '\"' || *p == '\\') *q++ = '\\';
    *q++ = *p++;
  }
  *q = 0;
}
void main(void) {
  char *data[]={
"#include <stdio.h>",
"char buffer[80];",
"void quote(char *p) {",
"  char *q = buffer;",
"  while (*p) {",
"    if (*p == '\\\"' || *p == '\\\\') *q++ = '\\\\';",
"    *q++ = *p++;",
"  }",
"  *q = 0;",
"}",
"void main(void) {",
"  char *data[]={",
"};",
"  int i;",
"  for (i = 0;i <= 11;i++)",
"    puts(data[i]);",
"  for (i = 0;i <= 23;i++) {",
"    quote(data[i]);",
"    printf(\"\\\"%s\\\"\",buffer);",
"    if (i < 23) puts(\",\"); else puts(\"\");",
"  }",
"  for (i = 12;i <= 23;i++)",
"    puts(data[i]);",
"}"
};
  int i;
  for (i = 0;i <= 11;i++)
    puts(data[i]);
  for (i = 0;i <= 23;i++) {
    quote(data[i]);
    printf("\"%s\"",buffer);
    if (i < 23) puts(","); else puts("");
  }
  for (i = 12;i <= 23;i++)
    puts(data[i]);
}