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

C statement to copy a string without using loop and library function..

2 Answers   Persistent, TCS,


write a program to count the number the same (letter/character foreg: 's') in a given sentence.

2 Answers  


How to return multiple values from a function?

7 Answers  


main() { int i = 0xff ; printf("\n%d", i<<2); } a. 4 b. 512 c. 1020 d. 1024

2 Answers   HCL,


To Write a C program to remove the repeated characters in the entered expression or in entered characters(i.e) removing duplicates. String contains only lowercase characters ['a'-'z']

0 Answers  






Write a C program to print ‘Campus Force training’ without using even a single semicolon in the program.

3 Answers   Wipro,


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,


Given an array of size N in which every number is between 1 and N, determine if there are any duplicates in it. You are allowed to destroy the array if you like.

21 Answers   ABC, eBay, Goldman Sachs, Google, HUP, Microsoft, TATA,


int swap(int *a,int *b) { *a=*a+*b;*b=*a-*b;*a=*a-*b; } main() { int x=10,y=20; swap(&x,&y); printf("x= %d y = %d\n",x,y); }

1 Answers  


write a origram swaoing valu without 3rd variable

2 Answers  


main() { void swap(); int x=10,y=8; swap(&x,&y); printf("x=%d y=%d",x,y); } void swap(int *a, int *b) { *a ^= *b, *b ^= *a, *a ^= *b; }

2 Answers  


There are 21 people in a room. They have to form groups of 3 people each. How many combinations are possible? Write a C program to print the same.

1 Answers   TCS,


Categories