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

void main() { int i=10, j=2; int *ip= &i, *jp = &j; int k = *ip/*jp; printf(“%d”,k); }

1 Answers  


#include <stdio.h> main() { char * str = "hello"; char * ptr = str; char least = 127; while (*ptr++) least = (*ptr<least ) ?*ptr :least; printf("%d",least); }

1 Answers  


main() { char *p; p="%d\n"; p++; p++; printf(p-2,300); }

1 Answers  


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

1 Answers  


{ int *ptr=(int*)malloc(sizeof(int)); *ptr=4; printf("%d",(*ptr)+++*ptr++); }

4 Answers  






plz send me all data structure related programs

2 Answers  


how to return a multiple value from a function?

2 Answers   Wipro,


main() { int i; float *pf; pf = (float *)&i; *pf = 100.00; printf("\n %d", i); } a. Runtime error. b. 100 c. Some Integer not 100 d. None of the above

2 Answers   HCL, LG,


void main() { printf(“sizeof (void *) = %d \n“, sizeof( void *)); printf(“sizeof (int *) = %d \n”, sizeof(int *)); printf(“sizeof (double *) = %d \n”, sizeof(double *)); printf(“sizeof(struct unknown *) = %d \n”, sizeof(struct unknown *)); }

1 Answers  


#define prod(a,b) a*b main() { int x=3,y=4; printf("%d",prod(x+2,y-1)); }

1 Answers  


3) Int Matrix of certain size was given, We had few valu= es in it like this. =97=97=97=97=97=97=97=97=97=97=97 1 = | 4 | | 5 | &= nbsp; | 45 =97=97=97=97=97=97=97=97=97=97=97 &n= bsp; | 3 | 3 | 5 | = | 4 =97=97=97=97=97=97=97=97=97=97=97 34 |&nbs= p; 3 | 3 | | 12 | &= nbsp; =97=97=97=97=97=97=97=97=97=97=97 3 | &nbs= p; | 3 | 4 | = | 3 =97=97=97=97=97=97=97=97=97=97=97 3 | = ; | | | = ; 3 | =97=97=97=97=97=97=97=97=97=97=97 &= nbsp; | | 4 | = ; | 4 | 3 We w= ere supposed to move back all the spaces in it at the end. Note: = If implemented this prog using recursion, would get higher preference.

0 Answers   RoboSoft,


void pascal f(int i,int j,int k) { printf(“%d %d %d”,i, j, k); } void cdecl f(int i,int j,int k) { printf(“%d %d %d”,i, j, k); } main() { int i=10; f(i++,i++,i++); printf(" %d\n",i); i=10; f(i++,i++,i++); printf(" %d",i); }

1 Answers  


Categories