Print Source Code of C program
C program to print source code
In this section, we will learn how to write a C program to print source code of the same program in simple steps. We will perform the task with the help of predefined _FILE_ macro and also study about it in detail. This macro will help to extract the location of the C file.
Print Source Code as Output
To display the source code of a C program as the output itself, we can make use of the _FILE_ macro which will help in locating the C file in the memory.
_FILE_ macro will store the location of the C program file:
#include<stdio.h>
int main()
{
printf("%s",__FILE__);
}
The above mentioned code will give the location of this C program file. Similarly, we can write the code for displaying source code output using this predefined _FILE_ macro.
Example to display source code as output:
#include<stdio.h>
int main(void)
{
char c;
FILE *fp = fopen(__FILE__, "r");
do
{
c = fgetc(fp);
putchar(c);
}
while (c != EOF);
fclose(fp);
return 0;
}
Output:
#include<stdio.h>
int main(void)
{
char c;
FILE *fp = fopen(__FILE__, "r");
do
{
c = fgetc(fp);
putchar(c);
}
while (c != EOF);
fclose(fp);
return 0;
}
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Login/Signup to comment