How to display same source code as output in c programming?

December 26, 2024 1 Comment » Hits : 223





Program :

#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;
}

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;
}

Explanation :

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 -
c://tc/bin/file1.c

then

fp = fopen(__FILE__,"r");

will be expanded as -

fp = fopen("c://tc/bin/file1.c","r");

Incoming search terms:

  • Lena Neihart

    That is the precise blog for anybody who wants to search out out about this topic. You understand a lot its virtually exhausting to argue with you (not that I actually would want…HaHa). You undoubtedly put a brand new spin on a subject thats been written about for years. Great stuff, just great!