如何打印C程序本身的源代码?注意这与奎因问题不同。在这里,我们需要修改任何C程序,使其打印整个源代码。
null
解释 : 我们可以使用文件处理的概念将程序的源代码打印为输出。其想法是:显示来自您正在编写源代码的同一文件的内容。
C编程文件的位置包含在预定义的宏文件中。例如:
#include <stdio.h> int main() { // Prints location of C this C code. printf ( "%s" ,__FILE__); } |
上述程序的输出就是这个C文件的位置。 以下程序显示此特定C文件(源代码)的内容,因为_文件_包含此C文件在字符串中的位置。
// A C program that prints its source code. #include <stdio.h> int main( void ) { // We can append this code to any C program // such that it prints its source code. char c; FILE *fp = fopen (__FILE__, "r" ); do { c = fgetc (fp); putchar (c); } while (c != EOF); fclose (fp); return 0; } |
输出 :
// A C program that prints its source code. #include <stdio.h> int main(void) { // We can append this code to any C program // such that it prints its source code. char c; FILE *fp = fopen(__FILE__, "r"); do { c = fgetc(fp); putchar(c); } while (c != EOF); fclose(fp); return 0; }
注: 上述程序可能无法在线编译,因为fopen可能被阻止。
本文由 里沙夫·拉杰 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 贡献极客。组织 或者把你的文章寄到contribute@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。
如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END