About Me

My photo
Raipur, Chhattisgarh, India
Hi , I am Amit Thakur. I have worked as a QA Engineer for two years and as a Java Developer for one year in NIHILENT TECHNOLOGIES PVT. LTD., Pune.Currently I am working as DEAN (Research & Development) in Bhilai Institute of Technology, Raipur.

Tuesday, September 3, 2013

C Program to Display same Source Code as 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;
}
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");
[arrowlist]
  • __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 -
[/arrowlist]
c://tc/bin/file1.c
then
fp = fopen(__FILE__,"r");
will be expanded as -
fp = fopen("c://tc/bin/file1.c","r");

No comments: