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
- Apply for Computer Science Internship
- Practice BCA MCQs
- Check Computer Science Books
- Apply for C Internship
- Practice Computer Science MCQs