How to write a program to print its own source code?

How to write a program to print its own source code?

I will given you 2 examples those program print its own source code.
In C:

#include 
main()
{
   FILE *fd;
   int c;
   fd= fopen("./file.c","r");
   while ( (c=fgetc(fd)) != EOF)
   {
      printf("%c", c);
   }
   fclose(fd);
} 

In C++:

#include
#include
#include
#include
#include
void main()
{
   char ch;
   clrscr();
   fstream fout;
   fout.open("own_source_code.c",ios::in);
   if(!fout)
   {
      printf("\n can't open");
      exit(0);
   }
      while(ch!=EOF)
      {
         ch=fout.get();
         cout<

By:Vivek Kr. Agarwal
Date:2003-01-09 00:00:00

Post Your Answers


User Email:

User Name:
Answers:

Related C Subjective Links

C Subjective interview questions and answers for experienced and fresher