Team LiB
Previous Section Next Section

1.3. A Word About Comments

1.3. 关于注释

Before our programs get much more complicated, we should see how C++ handles comments. Comments help the human readers of our programs. They are typically used to summarize an algorithm, identify the purpose of a variable, or clarify an otherwise obscure segment of code. Comments do not increase the size of the executable program. The compiler ignores all comments.


In this book, we italicize comments to make them stand out from the normal program text. In actual programs, whether comment text is distinguished from the text used for program code depends on the sophistication of the programming environment.


There are two kinds of comments in C++: single-line and paired. A single-line comment starts with a double slash (//). Everything to the right of the slashes on the current line is a comment and ignored by the compiler.

C++ 中有单行注释和成对注释两种类型的注释。单行注释以双斜线(//)开头,行中处于双斜线右边的内容是注释,被编译器忽略。

The other delimiter, the comment pair (/* */), is inherited from the C language. Such comments begin with a /* and end with the next */. The compiler treats everything that falls between the /* and */ as part of the comment:

另一种定界符,注释对(/* */),是从 C 语言继承过来的。这种注释以“/*”开头,以“*/”结尾。编译器把落入注释对“/**/”之间的内容作为注释:

   #include <iostream>
   /* Simple main function: Read two numbers and write their sum */
   int main()
       // prompt user to enter two numbers
       std::cout << "Enter two numbers:" << std::endl;
       int v1, v2;           // uninitialized
       std::cin >> v1 >> v2; // read input
       return 0;

A comment pair can be placed anywhere a tab, space, or newline is permitted. Comment pairs can span multiple lines of a program but are not required to do so. When a comment pair does span multiple lines, it is often a good idea to indicate visually that the inner lines are part of a multi-line comment. Our style is to begin each line in the comment with an asterisk, thus indicating that the entire range is part of a multi-line comment.


Programs typically contain a mixture of both comment forms. Comment pairs generally are used for multi-line explanations, whereas double slash comments tend to be used for half-line and single-line remarks.


Too many comments intermixed with the program code can obscure the code. It is usually best to place a comment block above the code it explains.


Comments should be kept up to date as the code itself changes. Programmers expect comments to remain accurate and so believe them, even when other forms of system documentation are known to be out of date. An incorrect comment is worse than no comment at all because it may mislead a subsequent reader.


Comment Pairs Do Not Nest


A comment that begins with /* always ends with the next */. As a result, one comment pair cannot occur within another. The compiler error message(s) that result from this kind of program mistake can be mysterious and confusing. As an example, compile the following program on your system:

注释总是以 /* 开始并以 */ 结束。这意味着,一个注释对不能出现在另一个注释对中。由注释对嵌套导致的编译器错误信息容易使人迷惑。例如,在你的系统上编译下面的程序:

   #include <iostream>
    * comment pairs /* */ cannot nest.
    * "cannot nest" is considered source code,
    * as is the rest of the program
   int main()
       return 0;

When commenting out a large section of a program, it can seem easiest to put a comment pair around a region that you want to omit temporarily. The trouble is that if that code already has a comment pair, then the newly inserted comment will terminate prematurely. A better way to temporarily ignore a section of code is to use your editor to insert single-line comment at the beginning of each line of code you want to ignore. That way, you need not worry about whether the code you are commenting out already contains a comment pair.


Exercises Section 1.3

Exercise 1.7:

Compile a program that has incorrectly nested comments.


Exercise 1.8:

Indicate which, if any, of the following output statements, are legal.


   std::cout << "/*";
   std::cout << "*/";
   std::cout << /* "*/" */;

After you've predicted what will happen, test your answer by compiling a program with these three statements. Correct any errors you encounter.


Team LiB
Previous Section Next Section