This is a C Program to display its own source code as its output.
This program displays its own source code as its output.
1. Display the content from the same file you are writing the source code.
Here is source code of the C Program to display its own source code as its output.The C program is successfully compiled and run on a Linux system. The program output is also shown below.
/*
* C Program to Display its own Source Code as its Output
*/
#include <stdio.h>
int main()
{
FILE *fp;
char ch;
fp = fopen(__FILE__,"r");
do
{
ch = getc(fp);
putchar(ch);
}
while (ch != EOF);
fclose(fp);
return 0;
}
1. Open the file you are currently writing using statement fopen(__FILE__,”r”) and assign it to the pointer fp.
2. Scan the 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.
Output: /* * C Program to display its own source code as its output */ #include <stdio.h> int main() { FILE *fp; char ch; fp = fopen(__FILE__,"r"); do { ch = getc(fp); putchar(ch); } while (ch != EOF); fclose(fp); return 0; }
Sanfoundry Global Education & Learning Series – 1000 C Programs.
Here’s the list of Best Books in C Programming, Data-Structures and Algorithms
- Get Free Certificate of Merit in C Programming
- Participate in C Programming Certification Contest
- Become a Top Ranker in C Programming
- Take C Programming Tests
- Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Practice BCA MCQs
- Practice Computer Science MCQs
- Watch Advanced C Programming Videos
- Buy Computer Science Books
- Apply for Computer Science Internship