How do you write a program which produces its own source
code as its output?

Answer Posted / abhishek

#include <stdio.h>

static char prog[] = "#include <stdio.h>%c%cstatic char prog[] = %c%s%c;%c%cint main(void)%c{%c printf(prog, 10, 10, 34, prog, 34, 10, 10, 10, 10, 10, 10, 10);%c return 0;%c}%c";

int main(void)
{
printf(prog, 10, 10, 34, prog, 34, 10, 10, 10, 10, 10, 10, 10);
return 0;
}

Is This Answer Correct ?    1 Yes 2 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is a function in c?

577


int i=10; printf("%d %d %d", i, i=20, i);

1018


What is the symbol indicated the c-preprocessor?

699


Hi how many types of software editions are there and their difference (like home editions, enterprise, standard etc) can u please help me

1470


Can a function argument have default value?

676






can anyone please tell about the nested interrupts?

1678


How can I find out the size of a file, prior to reading it in?

625


What is the size of structure in c?

705


what is the difference between 123 and 0123 in c?

727


If you know then define #pragma?

678


Do pointers need to be initialized?

567


Want to know how to write a C program that connects to a MySQL server and checks if the InnoDB plug-in is installed on it. If so, your program should print the total number of disk writes by MySQL.

1524


What will the code below print when it is executed?   int x = 3, y = 4;         if (x = 4)                 y = 5;         else                 y = 2;         printf ("x=%d, y=%d ",x,y);

1360


If a variable is a pointer to a structure, then which operator is used to access data members of the structure through the pointer variable?

782


What is %s and %d in c?

594