GeekInterview.com
Series: Subject: Topic:
Question: 351 of 357

How do you write a program which produces its own source code as its output?

Asked by: Interview Candidate | Asked on: Sep 4th, 2005
Showing Answers 1 - 11 of 11 Answers
abhijit

Answered On : Jun 27th, 2005

Can i get the ans fr the question Question : How do you write a program which produces its own source code as its output?

Yes  1 User has rated as useful.
  
Login to rate this answer.
Anjaneyulu

Answered On : Jul 16th, 2005

/* Program to printf itself on console */ #include  int main(void) {  FILE *in;  clrscr();  if ((in = fopen("8.c", "r")) == NULL)  {  printf("Cannot open input file.n");  return 1;  }  while (!feof(in))  printf("%c", fgetc(in));  fclose(in);  getche();  return 0; } 

  
Login to rate this answer.
venu

Answered On : Jul 16th, 2005

Answer 

  
Login to rate this answer.
suhasini

Answered On : Jul 27th, 2005

Howdoes return1 and return 0 differ?

  
Login to rate this answer.
Santhanam

Answered On : Sep 3rd, 2005

'return 0' indicates program is ended with no error and 'return 1' indicates program is ended with error.

  
Login to rate this answer.
Harish Kumar

Answered On : Sep 12th, 2005

#include<stdio.h>  #include<string.h>#include<stdlib.h>int main(int args,char **argv) { char filename[20];int i=0,k=0;FILE *in; while(argv[0][i] != '\0')if(argv[0][i++] == '\\')k=i;printf("%d",k);for(i=0;argv[0][k] != '.';i++,k++){filename[i] = argv[0][k];}filename[i] = '\0';strcat(filename,".c");if ((in = fopen(filename, "r")) == NULL) { printf("Cannot open input file.n"); return 1; } while (!feof(in)) printf("%c", fgetc(in)); fclose(in); return 0; }

  
Login to rate this answer.
Sudhakar

Answered On : Oct 10th, 2005

Main() { char *a="main() { char *a=%c%s%c; char b='%c'; printf(a,b,a,b,b); }"; char b='"'; printf(a,b,a,b,b); }

  
Login to rate this answer.
wasim

Answered On : Jul 21st, 2006

#includemain(){         FILE *p;             p=fopen("filename","r");             if(p==null)exit(1);       while(!eof(p))     {            printf("%c",*p);               p++;      }getche();}

  
Login to rate this answer.
Guest

Answered On : Aug 3rd, 2006

Hi,  There are So many complex methods for that but i write the most effective and simple one  just write this ..// program name avi.c#include<stdlib.h>void main(){ //// write ur program logic heregetch();system("type avi.c");}by the use of this system function from stdlib.h u can run any dos command from ur c program but please check the output for this only by running the exe of ur program ( in this case avi.exe)Regards ,Avinash Chaturvedi( Avi )

Yes  3 Users have rated as useful.
  
Login to rate this answer.
Manoj

Answered On : Mar 14th, 2007

All the solutions above are reading the source code with the files concepts.find a solution where you do not have a source file to read the contents.There is a macro or an attiribute which tells the compiler the name of the program, function or the attribute. You can use that to get the source code. try out...!!!

  
Login to rate this answer.
praveensaini23

Answered On : Jun 28th, 2010

View all answers by praveensaini23

#include <stdio.h>int main(){char ch;FILE *file;file=fopen("printitself.c","r");while((ch=fgetc(file))!=EOF)printf("%c",ch);return 0;}

  
Login to rate this answer.

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

Related Open Questions

Ads

Connect

twitter fb Linkedin GPlus RSS

Ads

Interview Question

 Ask Interview Question?

 

Latest Questions

Interview & Career Tips

Get invaluable Interview and Career Tips delivered directly to your inbox. Get your news alert set up today, Once you confirm your Email subscription, you will be able to download Job Inteview Questions Ebook . Please contact me if you there is any issue with the download.