C program to print source code as program output

This is possible to print source code as program output; in this program we used a trick to print the source code as program’s output. We used file handling here to print the source code as output. Read the program and their explanation.

C program to print source code as program output

/*program to print source code as program output.*/
 
#include <stdio.h>
 
int main(void)
{
    FILE *fp;
    char c;
 
    fp = fopen(__FILE__, "r");
 
    do
    {
        c=fgetc(fp);
        putchar(c);
    }
    while(c!=EOF);
 
    fclose(fp);
 
    return 0;
}

Output

#include <stdio.h>

int main(void)
{
FILE *fp;
char c;

fp = fopen(__FILE__, "r");

do
{
c=fgetc(fp);
putchar(c);
}
while(c!=EOF);

fclose(fp);

return 0;
}

Explanation

Its a trick by which you can print your current source code file, use __FILE__ to specify current file name and read the file (using file handling) and print it on screen, that will your output.

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.