C++ 注释的不正确嵌套

用以下程序举例:

#include <iostream>
/*
* 注释对 /* */ 不能嵌套
* 否则后面的字会被认为是源码,像剩余程序一样处理
*/

int main() {
    return 0;
}

命名为 exa.cpp,然后编译这个程序。

# g++ exa.cpp
exa.cpp:4:17: error: stray ‘\344’ in program
 * 注释对/* */不能嵌套
                 ^
exa.cpp:4:17: error: stray ‘\270’ in program
exa.cpp:4:17: error: stray ‘\215’ in program
exa.cpp:4:17: error: stray ‘\350’ in program
exa.cpp:4:17: error: stray ‘\203’ in program
exa.cpp:4:17: error: stray ‘\275’ in program
exa.cpp:4:17: error: stray ‘\345’ in program
exa.cpp:4:17: error: stray ‘\265’ in program
exa.cpp:4:17: error: stray ‘\214’ in program
exa.cpp:4:17: error: stray ‘\345’ in program
exa.cpp:4:17: error: stray ‘\245’ in program
exa.cpp:4:17: error: stray ‘\227’ in program
exa.cpp:5:1: error: stray ‘\342’ in program
 * “不能嵌套”几个字会被认为是源码,像剩余程序一样处理
 ^
exa.cpp:5:1: error: stray ‘\200’ in program
exa.cpp:5:1: error: stray ‘\234’ in program
exa.cpp:5:1: error: stray ‘\344’ in program
exa.cpp:5:1: error: stray ‘\270’ in program
exa.cpp:5:1: error: stray ‘\215’ in program
exa.cpp:5:1: error: stray ‘\350’ in program
exa.cpp:5:1: error: stray ‘\203’ in program
exa.cpp:5:1: error: stray ‘\275’ in program
exa.cpp:5:1: error: stray ‘\345’ in program
exa.cpp:5:1: error: stray ‘\265’ in program
exa.cpp:5:1: error: stray ‘\214’ in program
exa.cpp:5:1: error: stray ‘\345’ in program
exa.cpp:5:1: error: stray ‘\245’ in program
exa.cpp:5:1: error: stray ‘\227’ in program
exa.cpp:5:1: error: stray ‘\342’ in program
exa.cpp:5:1: error: stray ‘\200’ in program
exa.cpp:5:1: error: stray ‘\235’ in program
exa.cpp:5:1: error: stray ‘\345’ in program
exa.cpp:5:1: error: stray ‘\207’ in program
exa.cpp:5:1: error: stray ‘\240’ in program
exa.cpp:5:1: error: stray ‘\344’ in program
exa.cpp:5:1: error: stray ‘\270’ in program
exa.cpp:5:1: error: stray ‘\252’ in program
exa.cpp:5:1: error: stray ‘\345’ in program
exa.cpp:5:1: error: stray ‘\255’ in program
exa.cpp:5:1: error: stray ‘\227’ in program
exa.cpp:5:1: error: stray ‘\344’ in program
exa.cpp:5:1: error: stray ‘\274’ in program
exa.cpp:5:1: error: stray ‘\232’ in program
exa.cpp:5:1: error: stray ‘\350’ in program
exa.cpp:5:1: error: stray ‘\242’ in program
exa.cpp:5:1: error: stray ‘\253’ in program
exa.cpp:5:1: error: stray ‘\350’ in program
exa.cpp:5:1: error: stray ‘\256’ in program
exa.cpp:5:1: error: stray ‘\244’ in program
exa.cpp:5:1: error: stray ‘\344’ in program
exa.cpp:5:1: error: stray ‘\270’ in program
exa.cpp:5:1: error: stray ‘\272’ in program
exa.cpp:5:1: error: stray ‘\346’ in program
exa.cpp:5:1: error: stray ‘\230’ in program
exa.cpp:5:1: error: stray ‘\257’ in program
exa.cpp:5:1: error: stray ‘\346’ in program
exa.cpp:5:1: error: stray ‘\272’ in program
exa.cpp:5:1: error: stray ‘\220’ in program
exa.cpp:5:1: error: stray ‘\347’ in program
exa.cpp:5:1: error: stray ‘\240’ in program
exa.cpp:5:1: error: stray ‘\201’ in program
exa.cpp:5:1: error: stray ‘\357’ in program
exa.cpp:5:1: error: stray ‘\274’ in program
exa.cpp:5:1: error: stray ‘\214’ in program
exa.cpp:5:1: error: stray ‘\345’ in program
exa.cpp:5:1: error: stray ‘\203’ in program
exa.cpp:5:1: error: stray ‘\217’ in program
exa.cpp:5:1: error: stray ‘\345’ in program
exa.cpp:5:1: error: stray ‘\211’ in program
exa.cpp:5:1: error: stray ‘\251’ in program
exa.cpp:5:1: error: stray ‘\344’ in program
exa.cpp:5:1: error: stray ‘\275’ in program
exa.cpp:5:1: error: stray ‘\231’ in program
exa.cpp:5:1: error: stray ‘\347’ in program
exa.cpp:5:1: error: stray ‘\250’ in program
exa.cpp:5:1: error: stray ‘\213’ in program
exa.cpp:5:1: error: stray ‘\345’ in program
exa.cpp:5:1: error: stray ‘\272’ in program
exa.cpp:5:1: error: stray ‘\217’ in program
exa.cpp:5:1: error: stray ‘\344’ in program
exa.cpp:5:1: error: stray ‘\270’ in program
exa.cpp:5:1: error: stray ‘\200’ in program
exa.cpp:5:1: error: stray ‘\346’ in program
exa.cpp:5:1: error: stray ‘\240’ in program
exa.cpp:5:1: error: stray ‘\267’ in program
exa.cpp:5:1: error: stray ‘\345’ in program
exa.cpp:5:1: error: stray ‘\244’ in program
exa.cpp:5:1: error: stray ‘\204’ in program
exa.cpp:5:1: error: stray ‘\347’ in program
exa.cpp:5:1: error: stray ‘\220’ in program
exa.cpp:5:1: error: stray ‘\206’ in program
exa.cpp:6:2: error: expected unqualified-id before ‘/’ token
 */
  ^
exa.cpp:6:2: error: expected constructor, destructor, or type conversion before ‘/’ token

输出的报错...

如果不是已经知道了问题才看到报错,而是要从报错反推问题,实在会让人束手无策。

究其原因就是注释对 “/* */” 不可嵌套,“/*” 总会与后文遇到的第一个 “*/” 组成一对,“*/” 总会与前文最近的 “/*” 组成一对。

当然这是指每行都不存在分号的情况下,如果有分号,这一行不论遇到了什么都结束了。

几个例子:

cout << "/*";                  // 输出“/*”
cout << "*/";                  // 输出“*/”
cout << /* "*/" /* "/*" */;    // 输出“ /* ”




PS. 今天收到了简书送来的礼物,是一本宋词书,haha。那么以后只要有 idea 我就来更一波~