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 |
How we print the table of 2 using for loop in c programing?
plz send me all data structure related programs
main() { int i = 257; int *iPtr = &i; printf("%d %d", *((char*)iPtr), *((char*)iPtr+1) ); }
Sir... please give some important coding questions asked by product companies..
void main() { char a[]="12345\0"; int i=strlen(a); printf("here in 3 %d\n",++i); }
main() { char *p = “ayqm”; printf(“%c”,++*(p++)); }
29 Answers IBM, TCS, UGC NET, Wipro,
void main() { char far *farther,*farthest; printf("%d..%d",sizeof(farther),sizeof(farthest)); }
What is wrong with the following code? int *foo() { int *s = malloc(sizeof(int)100); assert(s != NULL); return s; }
Extend the sutherland-hodgman clipping algorithm to clip three-dimensional planes against a regular paralleiepiped
#include<conio.h> main() { int x,y=2,z,a; if(x=y%2) z=2; a=2; printf("%d %d ",z,x); }
How can i find first 5 natural Numbers without using any loop in c language????????
main() { int *ptr=(int*)malloc(sizeof(int)); *ptr=4; printf("%d",(*ptr)+++*ptr++); }