虽然这个问题看起来很复杂,但这个程序背后的概念很简单;显示您正在编写源代码的同一个文件中的内容。
在 C 编程中,有一个名为
__FILE__
的预定义宏,用于给出当前输入文件的名称。
#include <stdio.h> int main() { // location the current input file. printf("%s",__FILE__); }
C 程序显示自己的源代码
#include <stdio.h> int main() { FILE *fp; int c; // open the current input file fp = fopen(__FILE__,"r"); do { c = getc(fp); // read character putchar(c); // display character } while(c != EOF); // loop until the end of file is reached fclose(fp); return 0; }