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

Showing Answers 1 - 11 of 11 Answers

abhijit

  • 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?

Anjaneyulu

  • 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; 

  Was this answer useful?  Yes

venu

  • Jul 16th, 2005
 

answer 

  Was this answer useful?  Yes

suhasini

  • Jul 27th, 2005
 

Howdoes return1 and return 0 differ?

  Was this answer useful?  Yes

Santhanam

  • Sep 3rd, 2005
 

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

  Was this answer useful?  Yes

Harish Kumar

  • 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;
}

  Was this answer useful?  Yes

Sudhakar

  • 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); }

  Was this answer useful?  Yes

wasim

  • Jul 21st, 2006
 

#include<>

main()

{

         FILE *p;

             p=fopen("filename","r");

             if(p==null)exit(1);

       while(!eof(p))

     {

            printf("%c",*p);

               p++;

      }

getche();

}

  Was this answer useful?  Yes

Guest

  • 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 here

getch();

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 )

Manoj

  • 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...!!!

  Was this answer useful?  Yes

Give your answer:

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

Answer Question

Click here to Login / Register your free account


 
Send   Reset

 

Related Answered Questions

 

Related Open Questions