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

Answers were Sorted based on User's Feedback



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

Answer / ram

#include <stdio.h>
main()
{
FILE *fd;
int c;

fd= fopen("./file.c","r");
while ( (c=fgetc(fd)) != EOF)
{
printf("%c", c);
}

fclose(fd);
}

Is This Answer Correct ?    19 Yes 8 No

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

Answer / lijun li

#include <stdio.h>

main()
{
printf("%s\n", __FILE__);
FILE* fp = fopen(__FILE__, "r");

char buf[4096];
while (!feof(fp))
{
fgets(buf, 4096, fp);
printf("%s",buf);
}

fclose(fp);
}

Is This Answer Correct ?    11 Yes 4 No

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

Answer / aditya raj

/*In above solution, data.txt contains d source code. But
its not a good solution. Plz post some better solution */

main(s)
{
s="main(s){s=%c%s%c;printf(s,34,s,34);}";
printf(s,34,s,34);
}

Is This Answer Correct ?    9 Yes 4 No

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

Answer / aditya raj

#include<stdio.h>
main()
{
char n;
FILE *f;
f=fopen("data.txt","r");
while((fscanf(f,"%c",&n))!=EOF)
printf("%c",n);
fclose(f);
}

Is This Answer Correct ?    3 Yes 1 No

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

Answer / splurgeop

// PROGRAM which prints itself
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<fstream.h>
void main()
{
char ch;
clrscr();
fstream fout;
fout.open("itself.c",ios::in);
if(!fout)
{
printf("\n cant open");
exit(0);

}
while(ch!=EOF)
{
ch=fout.get();
cout<<ch;
}

fout.close();

}



//the file name is itself.c

Is This Answer Correct ?    9 Yes 9 No

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

Answer / chandu

Can u write above program in C

Is This Answer Correct ?    5 Yes 5 No

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

Answer / yogesh

#include<stdio.h>
int main(int argc,char *argv[])
{
FILE *fp;
char ch;
fp=fopen(argv[0],"r");
while(!feof(fp))
{
ch=fgetc(fp);
printf("%c",ch);
}
}

Compile the program as cc -o filename filename.c
and run as
./filename

Is This Answer Correct ?    3 Yes 4 No

Post New Answer

More C Code Interview Questions

plz send me all data structure related programs

2 Answers  


main() { int i=-1; -i; printf("i = %d, -i = %d \n",i,-i); }

1 Answers  


plz send me all data structure related programs

2 Answers  


main() { int x=5; clrscr(); for(;x==0;x--) { printf("x=%d\n”", x--); } } a. 4, 3, 2, 1, 0 b. 1, 2, 3, 4, 5 c. 0, 1, 2, 3, 4 d. none of the above

3 Answers   HCL,


Give a one-line C expression to test whether a number is a power of 2.

10 Answers   Microsoft,


write a c-program to find gcd using recursive functions

5 Answers   HTC, Infotech,


What is the subtle error in the following code segment? void fun(int n, int arr[]) { int *p=0; int i=0; while(i++<n) p = &arr[i]; *p = 0; }

1 Answers  


int main() { int x=10; printf("x=%d, count of earlier print=%d", x,printf("x=%d, y=%d",x,--x)); getch(); } ================================================== returns error>> ld returned 1 exit status =================================================== Does it have something to do with printf() inside another printf().

2 Answers  


int i; main(){ int t; for ( t=4;scanf("%d",&i)-t;printf("%d\n",i)) printf("%d--",t--); } // If the inputs are 0,1,2,3 find the o/p

2 Answers  


void main() { if(~0 == (unsigned int)-1) printf(“You can answer this if you know how values are represented in memory”); }

1 Answers  


What is the difference between proc means and proc tabulate ? explain with a simple example when you have to use means or tabulate?

1 Answers  


How to read a directory in a C program?

4 Answers  


Categories