ADVERTISEMENT
ADVERTISEMENT

Home » C solved programs » C puzzles Programs

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.

ADVERTISEMENT
ADVERTISEMENT


Comments and Discussions!



ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.