C Program to Display Its Own Source Code as Output

This video tutorial explains how to write a c / c++ program which produces its own source code as output.
You will learn how to use __FILE__ macro to get the current working file name and how to read and printout the contents of the opened file in detail withe example.



Source code for this video tutorial

#include <stdio.h>
#include <stdlib.h>

int main()
{
   FILE *fp;
   char ch;

   fp = fopen(__FILE__,"r");
   if( fp == NULL){
    printf("File is not opened successfully");
   }else{
    do{
        ch = fgetc(fp);
        putchar(ch);
    }while(ch != EOF);

    fclose(fp);
   }

    return 0;
}



Back To Top