C程序的注释用/*...*/来表示。请写一个程序,将输入的C程序源代码中的注释去掉,输出去掉注释之后的源代码放在一个文档里
时间: 2024-03-13 12:46:34 浏览: 31
好的,以下是一个简单的C程序,用于去掉C程序源代码中的注释:
```c
#include <stdio.h>
int main() {
FILE *input_file, *output_file;
char c, last_c = '\0';
input_file = fopen("input.c", "r");
output_file = fopen("output.c", "w");
while ((c = fgetc(input_file)) != EOF) {
if (c == '/' && last_c == '/') {
// single-line comment, skip until end of line
while ((c = fgetc(input_file)) != '\n');
} else if (c == '*' && last_c == '/') {
// multi-line comment, skip until end of comment
while ((c = fgetc(input_file)) != EOF) {
if (c == '/' && last_c == '*') {
break;
}
last_c = c;
}
} else {
fputc(last_c, output_file);
last_c = c;
}
}
fclose(input_file);
fclose(output_file);
return 0;
}
```
这个程序的具体作用是:读取一个名为 "input.c" 的输入文件,将其内容逐字符地读入,当发现 "//" 或 "/*" 时,跳过注释的部分,将不是注释的部分写入名为 "output.c" 的输出文件中。
注意:这个程序只是一个简单的示例,可能无法处理所有情况,并且可能会删除掉不应该删除的部分。在实际的开发中,应该使用更为严谨和完善的方法来去除注释。