C编程-多行注释

5
如何忽略多行注释符号内部的另一个多行注释?
假设我想将整个代码放入注释中,以便测试代码中的其他内容。
/* This is my first comment */
printf("\n This is my first print command");

/* This is my second comment */
printf("\n This is my second print command");

如果我执行
/* 

/* This is my first comment */
printf("\n This is my first print command");

/* This is my second comment */
printf("\n This is my second print command");

*/

这个出现了错误。


2
大多数现代集成开发环境都有工具,可以通过单击鼠标来帮助您进行块注释。正如下面的回答评论所说,您可能无法做到这一点。 - Tim Biegeleisen
此外,这也是不使用多行注释的一个好理由。 - Iharob Al Asimi
3个回答

9
您期望的是多行注释可以嵌套。引用标准C11第6.4.9章节的直接语句,除了在字符常量、字符串字面值或注释中,字符"/*"引入注释,在这样的注释中只检查多字节字符和找到终止它的"*/"字符。因此,/* ... */注释不支持嵌套。作为解决方法,您可以使用条件编译块,例如:
#if 0
.
.
.
.
#endif 

要将整个代码块注释掉。


1
谢谢 :) StackOverflow 让我学到了很多东西,感到非常开心。我以前从未使用过 # if 0 ..... #endif,所以非常感谢你 :) - Amninder Singh

2
你可以做的是:
#if 0
/Code that needs to be commented/
#endif

1
我猜你想注释一些包含注释的代码。
你可以使用条件编译来实现这个目的。
#if 0
/* This is my first comment */
printf("\n This is my first print command");

/* This is my second comment */
printf("\n This is my second print command");
#endif

#if 0#endif 之间的所有内容都将被编译器忽略,就像它是一条注释一样。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接