How do you write a program which produces its own source
code as its output?
Answers were Sorted based on User's Feedback
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 |
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 |
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 |
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 |
plz send me all data structure related programs
main() { int i=-1; -i; printf("i = %d, -i = %d \n",i,-i); }
plz send me all data structure related programs
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
Give a one-line C expression to test whether a number is a power of 2.
write a c-program to find gcd using recursive functions
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; }
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().
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
void main() { if(~0 == (unsigned int)-1) printf(“You can answer this if you know how values are represented in memory”); }
What is the difference between proc means and proc tabulate ? explain with a simple example when you have to use means or tabulate?
How to read a directory in a C program?