Printing source code of a C program itself

By | November 27, 2016

Problem Statement:

Display the current running program as it is.

Printing source code of a C program itself output 1

                                                                           Printing source code of a C program itself output

Problem Solution: Read the same program File.

Example: sampleTestProgram.c

Solution Language: Using C

__FILE__ is standard predefined macros are specified by the relevant language.This is the path by which the preprocessor opened the file, not the short name specified in ‘#include’.

FYI: All pre defined macros start with double underscores. “C Program”.

/* Source Code */

#include <stdio.h>

int main()

{

   /* File Pointer declaration */

   char c;

   FILE *fp;

   fp = fopen(__FILE__,"r");

   /*It opens Current C File */

  do

  {

    c = getc(fp); /*it Gets each character*/

    putchar(c);

    /*it prints each Character */

   }   while(c != EOF);

   /*it loops Till End of the program File */

   fclose(fp);

   /*It closes File handle fp */

   return 0;

}

c Program Compilation and Running It’s Executable.

Printing source code of a C program itself output

Printing source code of a C program itself output

If you want to Compile and Run program, follow the steps mentioned in the Article Compile C and C++ Programs

If you like Simplylearntopics.com and would like to contribute, you can Send article to [email protected]”.It will start appearing on the SimplyLearnTopics.com main page and help other Learners.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

The following two tabs change content below.

SRINIVAS DARIPELLI

Myself SRINIVAS DARIPELLI. I have 15+ Years of Experience in Programming worked on multiple technologies.Apart from it,I am a blogger, writer, editor, artist and dad 🙂 .I believe in reality.I love to share the Helpful things around the Technology. Feel free to connect with me

One thought on “Printing source code of a C program itself

  1. Sonny Sulistyo Hartanto

    Udah ada perintah cat linux, type windows,, 😀 but it’s a nice try,, (y)

    Reply

Leave a Reply