C Program to Display its own Source Code as Output

@codevarsity
 · 
2.1k views
 · 

In this sample C program, we'll learn to display source of the program using __FILE__ macro.

Code

#include <stdio.h>
int main(){
   printf("%s",__FILE__);
}

Output

#include <stdio.h>
int main() {
    FILE *fp;
    char c;
    fp = fopen(__FILE__,"r");
    do {
         c = getc(fp);
         putchar(c);
    }
    while(c != EOF);
    fclose(fp);
    return 0;
}

Comments