C program to display source code as output

Program to display source code

C program to display its own source code as output

We can use the concepts of file handling to print the source code of the program as output. The idea being: displaying the content from the same file you are writing the source code.

The location of a C programming file is contained inside a predefined macro __FILE__.
For example:

#include <stdio.h>
void main()

    FILE *fp;
    char ch;
printf("\n\t\t\t...Welcome To EduNews.Tech...");
    fp = fopen(__FILE__,"r");
        ch = getc(fp);
     while (ch != EOF);


Program Output:

source code

Program Explanation:

  1. Open the file you are currently writing using statement fopen(__FILE__,”r”) and assign it to the pointer fp.
  2. Scan every character of the file and store it in the variable ch. Print it using statement putchar(ch).
  3. Do step 2 until EOF (end of file).
  4. Then close the file and exit.

I hope this post helps you to understand how to print its own source code and its implementation in C programming language.

Keep coding 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *