C语言教程
C语言控制语句
C语言函数
C语言数组
C语言指针
C语言字符串
C语言数学函数
C语言结构
C语言文件处理
C预处理器

C 显示自己的源代码作为输出的程序

将自己的源代码显示为输出的 C 程序

在本例中,您将学习使用 __FILE__ 宏来显示程序的源代码。
要理解此示例,您应该了解以下C 编程 主题:
C 预处理器和宏 C 文件处理
虽然这个问题看起来很复杂,但这个程序背后的概念很简单;显示您正在编写源代码的同一个文件中的内容。
在 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;
}
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4