Interview Questions   Tutorials   Discussions   Programs   

C - Write a C program to print the program itself ?




424
views
asked marvit August 18, 2014 10:01 AM  

Write a C program to print the program itself ?


           

1 Answers



 
answered By bhjahnavi   0  
the program to print itself - this can be done in many ways  one of the method is using files 
#include<stdio.h>
void main()
{
FILE *fp;
char ch;
clrscr();
fp=fopen("file.c","r");
ch=getc(fp);
while(ch!=EOF)
{
putchar(ch);
ch=getc(fp);
}
getch();
} 
and the other  way is as follows
#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);
    
    return 0;
}
flag   
   add comment

Your answer

Join with account you already have

FF

Preview


Ready to start your tutorial with us? That's great! Send us an email and we will get back to you as soon as possible!

Alert