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

    
#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.








Was this page helpful? YES NO

Are you a blogger? Join our Blogging forum.



Comments and Discussions


We are using Google to publish ads on our website; Google has its own privacy policies. They may save log, cookies on your system. Google may also collect information of your system like IP address, region, city, country. For more details please go through the Google’s privacy policy.