0

I am trying to write a quine based off the Java code in this Wikipedia article. Here is my code so far:

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>

void print(int x)
{
    char * buf = itoa(x,NULL,10);
    puts(buf);
}

int main(void)
{
    rsize_t opening = 15;
    rsize_t i = 0;
    char  * q[] = 
    {
"#include <stdio.h>",
"#include <stdlib.h>",
"#include <stdint.h>",
"",
"void printf(int x)",
"{",
"   char * buf = itoa(x,NULL,10);",
"   puts(buf);",
"}",
"",
"int main(void)",
"{",
"   rsize_t opening = 15",
"   rsize_t i = 0;",
"   char  * q[] =",
"   {",
"   };",
"   while (i <= opening)",
"   {",
"       printf(q[i]);putchar(10);i++;",
"   }",
"   i = 0;",
"   while (q[i] != NULL)",
"   {",
"       putchar(34);printf(q[i]);putchar(34);putchar(44);putchar(10);i++;",
"   }",
"   i = opening+1;",
"   while (q[i] != NULL)",
"   {",
"       printf(q[i]);putchar(10);i++;",
"   }",
"   return 0;",
"}",
    };
    while (i <= opening)
    {
        printf(q[i]);putchar(10);i++;
    }
    i = 0;
    while (q[i] != NULL)
    {
        putchar(34);printf(q[i]);putchar(34);putchar(44);putchar(10);i++;
    }

    i = opening+1;
    while ( q[i] != NULL )
    {
        printf(q[i]);putchar(10);i++;
    }
    return 0;
}

The output of this program is the following:

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>

void printf(int x)
{
        char * buf = itoa(x,NULL,10);
        puts(buf);
}

int main(void)
{
        rsize_t opening = 15
        rsize_t i = 0;
        char  * q[] =
        {
"#include <stdio.h>",
"#include <stdlib.h>",
"#include <stdint.h>",
"",
"void printf(int x)",
"{",
"       char * buf = itoa(x,NULL,10);",
"       puts(buf);",
"}",
"",
"int main(void)",
"{",
"       rsize_t opening = 15",
"       rsize_t i = 0;",
"       char  * q[] =",
"       {",
"       };",
"       while (i <= opening)",
"       {",
"               printf(q[i]);putchar(10);i++;",
"       }",
"       i = 0;",
"       while (q[i] != NULL)",
"       {",
"               putchar(34);printf(q[i]);putchar(34);putchar(44);putchar(10);i++;",
"       }",
"       i = opening+1;",
"       while (q[i] != NULL)",
"       {",
"               printf(q[i]);putchar(10);i++;",
"       }",
"       return 0;",
"}",
"",

As we can see the program just stops printing just after it prints the last newline wrapped in quotation marks and followed by a comma: ("",). It was supposed to print the enclosing bracket (};) for the array (char * q[]) followed by all three while loops without them being wrapped in visible quotation marks to complete printing the quine. In the past, I had to shorten lines of code to allow the quine to finish printing. So it surprises me I am having this kind of problem with writing a quine once again.

Tanveer Salim
  • 27
  • 1
  • 1
  • 9

0 Answers0