为什么我们不能将一个评论嵌套在另一个评论中?
why can't we nest one comment inside another comment?
/*
/**hiiii**/
*/
说我这样嵌套评论,这有什么问题吗?一开始我们有 /*
,然后里面有一些字符串,最后我们在最后有 */
,那么这里的错误是什么?
/**hiiii**/
行末尾的 */ 结束注释。
这样做:
/*
//*Hiiii*
*/
C 标准对此非常明确。 /*...*/
样式注释不嵌套,因为语言语法不允许嵌套.
引用 C11
,第 6.4.9 章,评论,(强调我的)
Except within a character constant, a string literal, or a comment, the characters /*
introduce a comment. The contents of such a comment are examined only to identify
multibyte characters and to find the characters */
that terminate it.83)
以及相关的脚注,
83) Thus, /* ... */
comments do not nest.
详细说明,如您的情况,
/* <-- Comments starts here
/**hiiii**/ <-- Found the ending */, so comment ends.
*/ <-- problem here....
- 在第一行,
/*
开始评论。
- 在第二行开始
/*
不会 开始 new 评论,如前所述。
- 第二行结尾,
*/
结束注释。因此,在第三行,*/
产生了错误。
编译器在看到 /*
后查找 */
。一旦它找到更接近 */
的评论,一切就都完成了。从 /*
到第一个 */
的所有内容都被视为评论。所以,
/*
/**hiiii**/
发表评论。
第二个 */
前面没有 /*
,因此会导致错误。
why can't we nest one comment inside another comment?
因为语言设计者可怜语言实现者
解析不可嵌套的注释非常容易;解析可嵌套注释不太容易。
// pseudo code for unnestable comments
is the character a '*'? YES => Is the next one a '/'? YES END COMMENTS
else STILL INSIDE A COMMENT
代码可以 有效地 具有 #if 0
的嵌套注释行为。因此不需要扩展 /* */
行为。
#if 0
blah blah
#if 0
blah blah
#endif
blah blah
#endif
/*
引入的 C 注释不嵌套。这个决定是由最初的设计者在 40 多年前做出的,所有 C 标准都强制执行。尽管一些较旧的编译器支持使用命令行开关嵌套此类注释,但强烈建议遵守标准做法并考虑注释在第一次出现字符 */
.
时结束
请注意,/
和 *
可以通过转义换行符分隔(\
后跟换行符:
int i = 3; /\
* This is a comment (SO syntax coloring is not perfect ;-) *\
/ printf("%d\n", i); // prints 3
另请注意,通过 /*
*/
将代码变成注释来注释代码是个坏主意。如果您的代码包含 C 注释,这将失败,甚至可能静默失败:
int i = 3, j = 6;
/* printf("debug: i=%d\n", i); /* check the value of i */
printf("debug: j=%d\n", j); // check the value of j */
在上面的代码中,第二个 printf
没有被 注释 。
你可以想象更棘手的情况:
/*
// list all C source files in subdirectories
system("ls */*.[ch]");
*/
注释代码块有两种有效的方法:
使用 #if 0
/ #endif
预处理器指令。通过将 #if 0
更改为 #if 1
.
,可以嵌套并轻松 取消注释
在块中每行的开头插入 //
。这可以是嵌套,意思是你可以通过在每一行的开头再次插入//
来注释一个更大的块。
/*
/**hiiii**/
*/
说我这样嵌套评论,这有什么问题吗?一开始我们有 /*
,然后里面有一些字符串,最后我们在最后有 */
,那么这里的错误是什么?
/**hiiii**/
行末尾的 */ 结束注释。
这样做:
/*
//*Hiiii*
*/
C 标准对此非常明确。 /*...*/
样式注释不嵌套,因为语言语法不允许嵌套.
引用 C11
,第 6.4.9 章,评论,(强调我的)
Except within a character constant, a string literal, or a comment, the characters
/*
introduce a comment. The contents of such a comment are examined only to identify multibyte characters and to find the characters*/
that terminate it.83)
以及相关的脚注,
83) Thus,
/* ... */
comments do not nest.
详细说明,如您的情况,
/* <-- Comments starts here
/**hiiii**/ <-- Found the ending */, so comment ends.
*/ <-- problem here....
- 在第一行,
/*
开始评论。 - 在第二行开始
/*
不会 开始 new 评论,如前所述。 - 第二行结尾,
*/
结束注释。因此,在第三行,*/
产生了错误。
编译器在看到 /*
后查找 */
。一旦它找到更接近 */
的评论,一切就都完成了。从 /*
到第一个 */
的所有内容都被视为评论。所以,
/*
/**hiiii**/
发表评论。
第二个 */
前面没有 /*
,因此会导致错误。
why can't we nest one comment inside another comment?
因为语言设计者可怜语言实现者
解析不可嵌套的注释非常容易;解析可嵌套注释不太容易。
// pseudo code for unnestable comments
is the character a '*'? YES => Is the next one a '/'? YES END COMMENTS
else STILL INSIDE A COMMENT
代码可以 有效地 具有 #if 0
的嵌套注释行为。因此不需要扩展 /* */
行为。
#if 0
blah blah
#if 0
blah blah
#endif
blah blah
#endif
/*
引入的 C 注释不嵌套。这个决定是由最初的设计者在 40 多年前做出的,所有 C 标准都强制执行。尽管一些较旧的编译器支持使用命令行开关嵌套此类注释,但强烈建议遵守标准做法并考虑注释在第一次出现字符 */
.
请注意,/
和 *
可以通过转义换行符分隔(\
后跟换行符:
int i = 3; /\
* This is a comment (SO syntax coloring is not perfect ;-) *\
/ printf("%d\n", i); // prints 3
另请注意,通过 /*
*/
将代码变成注释来注释代码是个坏主意。如果您的代码包含 C 注释,这将失败,甚至可能静默失败:
int i = 3, j = 6;
/* printf("debug: i=%d\n", i); /* check the value of i */
printf("debug: j=%d\n", j); // check the value of j */
在上面的代码中,第二个 printf
没有被 注释 。
你可以想象更棘手的情况:
/*
// list all C source files in subdirectories
system("ls */*.[ch]");
*/
注释代码块有两种有效的方法:
使用
#if 0
/#endif
预处理器指令。通过将#if 0
更改为#if 1
. ,可以嵌套并轻松 取消注释
在块中每行的开头插入
//
。这可以是嵌套,意思是你可以通过在每一行的开头再次插入//
来注释一个更大的块。