Pages - Menu

Saturday, 19 July 2014

Quine in c

A program that prints itself is called Quine program

Concept:


Open the same file in read mode and start reading and printing from the file character by character till END OF FILE is reached Here is the code:

#include<stdio.h>
int main()
{
FILE *fp;
char ch;
fp=fopen("selfprint.c","r");
/* Enter your program name here*/
while((ch=fgetc(fp))!=EOF)    
printf("%c",ch);
fclose(fp);    
return(1);
}

Another code is here



Concept:


The char pointer "program" is used to store the string and %c and %s are format specifiers for char and string arguments
.
#include <stdio.h>
char *program = "#include <stdio.h>%cchar *program = %c%s%c;%cint main()%c{%cprintf(program, 10, 34, program, 34, 10, 10, 10, 10, 10, 10);%c    return 0;%c}%c";
int main()
{
        printf(program, 10, 34, program, 34, 10, 10, 10, 10, 10, 10);
/*printf function print output to console 10 is ASCII code for newline and 34 for " */
        return 0;
}

No comments:

Post a Comment