C Program to Display same Source Code as Output
Program :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | #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; } |
Output :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | #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; } |
Explanation :
1 | fp = fopen(__FILE__,"r"); |
- __FILE__ is Standard Predefined Macros in C Programming.
- This macro will expand to the name of current file path.
- Suppose we have saved this source code at path -
1 | c://tc/bin/file1.c |
then
1 | fp = fopen(__FILE__,"r"); |
will be expanded as -
1 | fp = fopen("c://tc/bin/file1.c","r"); |