Popular Tutorials
Start Learning CCertification Courses
Created with over a decade of experience and thousands of feedback.
Python online compiler
JavaScript online compiler
SQL online compiler
Java online compiler
HTML online compiler
C online compiler
C++ online compiler
C# online compiler
PHP online compiler
Swift online compiler
Kotlin online compiler
TypeScript online compiler
Go online compiler
Rust online compiler
Scala online compiler
Dart online compiler
R online compiler
Ruby online compiler
C Program to Display its own Source Code as Output
In this example, you'll learn to display source of the program using __FILE__ macro.
To understand this example, you should have the knowledge of the following C programming topics:
Though this problem seems complex, the concept behind this program is straightforward; display the content from the same file you are writing the source code.

In C programming, there is a predefined macro named __FILE__ that gives the name of the current input file.
#include <stdio.h>
int main() {
// location the current input file.
printf("%s",__FILE__);
}
C program to display its own source code
#include <stdio.h>
int main() {
FILE *fp;
int c;
// open the current input file
fp = fopen(__FILE__,"r");
do {
c = getc(fp); // read character
putchar(c); // display character
}
while(c != EOF); // loop until the end of file is reached
fclose(fp);
return 0;
}
Did you find this article helpful?