Print their own source code as output in C.
1. Open the file you are currently writing using statement fopen(__FILE__,”r”) and assign it to the pointer fp.
2. Scan the every character of the file and store it in the variable ch. Print it using statement putchar(ch).3. Do step 2 until EOF (end of file).
4. Then close the file and exit.
Example
code:
/** C Program to Display its own Source Code as its Output*/#include <stdio.h>int main()
{FILE *fp;
char ch;
fp = fopen(__FILE__,"r");
do{ch = getc(fp);
putchar(ch);
}while (ch != EOF);
fclose(fp);
return 0;
}
output
Output:
/*
* C Program to display its own source code as its output
*/
#include <stdio.h>
int main()
{
FILE *fp;
char ch;
fp = fopen(__FILE__,"r");
do
{
ch = getc(fp);
putchar(ch);
}
while (ch != EOF);
fclose(fp);
return 0;
}
No comments:
Post a Comment