14 Nov 2019

  • November 14, 2019
  • Amitraj
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.


/*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.


Translate

Popular Posts