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

Write a Program that Inputs 10 Numbers in an Array and Show the Maximum Number

2 Answers   Ace Info,


struct Foo { char *pName; char *pAddress; }; main() { struct Foo *obj = malloc(sizeof(struct Foo)); clrscr(); obj->pName = malloc(100); obj->pAddress = malloc(100); strcpy(obj->pName,"Your Name"); strcpy(obj->pAddress, "Your Address"); free(obj); printf("%s", obj->pName); printf("%s", obj->pAddress); } a. Your Name, Your Address b. Your Address, Your Address c. Your Name Your Name d. None of the above

2 Answers   HCL,


main() { register int a=2; printf("Address of a = %d",&a); printf("Value of a = %d",a); }

3 Answers  


find simple interest & compund interest

2 Answers  


void ( * abc( int, void ( *def) () ) ) ();

1 Answers  






Is the following code legal? typedef struct a { int x; aType *b; }aType

1 Answers  


Program to Delete an element from a doubly linked list.

4 Answers   College School Exams Tests, Infosys,


#include"math.h" void main() { printf("Hi everybody"); } if <stdio.h> will be included then this program will must compile, but as we know that when we include a header file in "" then any system defined function find its defination from all the directrives. So is this code of segment will compile? If no then why?

2 Answers  


write a program in c language to get the value of arroy keys pressed and display the message which arrow key is pressed?

1 Answers  


#define DIM( array, type) sizeof(array)/sizeof(type) main() { int arr[10]; printf(“The dimension of the array is %d”, DIM(arr, int)); }

1 Answers  


Write a complete program that consists of a function that can receive two numbers from a user (M and N) as a parameter. Then print all the numbers between the two numbers including the number itself. If the value of M is smaller than N, print the numbers in ascending flow. If the value of M is bigger than N, print the numbers in descending flow. may i know how the coding look like?

2 Answers  


main() { int i = 257; int *iPtr = &i; printf("%d %d", *((char*)iPtr), *((char*)iPtr+1) ); }

1 Answers   CSC,


Categories