
给定的任务是打印编写的C程序本身。
我们必须编写一个C程序,它将打印自身。因此,我们可以在C中使用文件系统来打印我们编写代码的文件的内容,就像我们在“code 1.c”文件中编写代码一样,所以我们以读模式打开文件,并读取文件的所有内容,并将结果打印在输出屏幕上。
但是,在以读模式打开文件之前,我们必须知道我们正在编写代码的文件的名称。因此,我们可以使用“__FILE__”这个宏,默认情况下返回当前文件的路径。
“__FILE__”宏的示例
#include<stdio.h>
int main() {
printf(“%s”, __FILE__);
}上述程序将打印代码所在文件的源代码
思远企业网站管理系统是由思远负责人结合多年的开发精髓为企业量身订做的一套全国通用版本的企业网站 管理系统,该系统体积小,代码执行速度快,用户操作相当简单而深受大家的喜爱。 版本说明:程序采用asp.net(c#)+access(节约企业资源) 1:网站由后台全静态生成前台所有页面,简化操作,一键即可生成大型企业网站 2:网站栏目灵活控制:是为企业量身订做的企业网站,通过模板自由生成各行各业大型企业
0
宏__FILE__返回一个字符串,其中包含当前程序的路径,它被提及的位置。
因此,当我们将其合并到文件系统中以以只读模式打开代码所在的当前文件时,我们可以像这样做-
fopen(__FILE__, "r");
Start
Step 1-> In function int main(void)
Declare a character c
Open a FILE “file” “__FILE__” in read mode
Loop do-while c != End Of File
Set c = fgetc(file)
putchar(c)
Close the file “file”
Stop#include <stdio.h>
int main(void) {
// to print the source code
char c;
// __FILE__ gets the location
// of the current C program file
FILE *file = fopen(__FILE__, "r");
do {
//printing the contents
//of the file
c = fgetc(file);
putchar(c);
}
while (c != EOF);
fclose(file);
return 0;
}#include <stdio.h>
int main(void) {
// to print the source code
char c;
// __FILE__ gets the location
// of the current C program file
FILE *file = fopen(__FILE__, "r");
do {
//printing the contents
//of the file
c = fgetc(file);
putchar(c);
}
while (c != EOF);
fclose(file);
return 0;
}以上就是打印一个C程序本身的源代码的详细内容,更多请关注php中文网其它相关文章!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号