Cplusplus 简明教程

Comments in C++

C++ Comments

程序注释是可以包含在 C++ 代码中的解释性语句。这些注释有助于任何人阅读源代码。所有编程语言都允许某种形式的注释。

Program comments are explanatory statements that you can include in the C++ code. These comments help anyone reading the source code. All programming languages allow for some form of comments.

Types of C++ Comments

C 支持两种类型的注释: single-line commentsmulti-line commentsC compiler 忽略任何注释中可用的所有字符。

C supports two types of comments: single-line comments and multi-line comments. All characters available inside any comment are ignored by the C compiler.

C++ 注释类型在下一节中进行了详细解释:

The types of C++ comments are explained in detail in the next sections:

1. C++ Single-line Comments

单行注释以 // 开头,一直延伸到行的末尾。这些注释只能持续到行的末尾,然后下一行将生成新的注释。

A single-line comment starts with //, extending to the end of the line. These comments can last only till the end of the line, and the next line leads to a new comment.

以下语法展示了如何在 C++ 中使用单行注释:

The following syntax shows how to use a single-line comment in C++:

// Text to be commented

在以下示例中,我们正在创建单行注释 −

In the following example, we are creating single-line comments −

#include <iostream>
using namespace std;

int main() {
  // this is a single line comment
  cout << "Hello world!" << endl;
  // for a new line, we have to use new comment sections
  cout << "This is second line.";

  return 0;
}

Output

Hello world!
This is second line.

2. C++ Multi-line Comments

多行注释以 / 开头,以 / 结尾。这些符号之间的任何文本仅被视为注释。

Multi-line comments start with / and end with /. Any text in between these symbols is treated as a comment only.

以下语法显示了在 C++ 中使用多行注释的方法:

The following syntax shows how to use a multi-line comment in C++:

/* This is a comment */

/*
  C++ comments can also
  span multiple lines
*/

在以下示例中,我们创建多行注释 −

In the following example, we are creating multi-line comments −

#include <iostream>
using namespace std;

int main() {
  /* Printing hello world!*/
  cout << "Hello World!" << endl;
  /*
  This is a multi-line comment
  Printing another message
  Using cout
  */
  cout << "Tutorials Point";

  return 0;
}

Output

Hello World!
Tutorials Point

Comments within Statements

我们还可以注释掉 C++ 程序中代码块内的特定语句。这两种类型的注释都能实现此目的。

We can also comment-out specific statements within a code block inside a C++ program. This is done using both types of comments.

Example

以下示例说明在语句中使用多行注释:

The following example explains the usage of multi-line comments within statements −

#include <iostream>
using namespace std;

int main() {
  cout << "This line" /*what is this*/ << " contains a comment" << endl;
  return 0;
}

Output

This line contains a comment

Example

以下示例说明在语句中使用单行注释:

The following example explains the usage of single-line comments within statements −

#include <iostream>
using namespace std;

int main() {
  cout << "This line"  // what is this
       << " contains a comment" << endl;
  return 0;
}

Output

This line contains a comment

Nesting Comments

在 /* 和 / comment, // characters have no special meaning. Within a // comment, / 和 */ 中没有任何特殊含义。因此,你可以将一种类型的注释“嵌套”在另一种类型中。

Within a /* and / comment, // characters have no special meaning. Within a // comment, / and */ have no special meaning. Thus, you can "nest" one kind of comment within the other kind.

Example

以下示例说明在注释中使用嵌套注释:

The following example explains the usage of comments within comments using nesting −

#include <iostream>
using namespace std;
int main() {
  /* Comment out printing of Hello World:

cout << "Hello World"; // prints Hello World

*/
  cout << "New, Hello World!";
  return 0;
}

Output

New, Hello World!

Single-line or Multi-line Comments - When to Use?

单行注释通常用于一般情况下的简短注释行。在代码中需要为算法说明提示时会使用这一点。

Single-line comments are generally used for short lines of comments in general. This is seen in cases where we have to mention a small hint for the algorithm in the code.

多行注释通常用于更长的注释行,其中整个注释行的可见性是必需的。注释的长度越长,多行注释需要更多条语句。

Multi-line comments are generally used for longer lines of comments, where the visibility of the whole comment line is necessary. The longer the length of the comment, the more number of statements are needed by the multi-line comments.

Purpose of Comments

注释在 C++ 中用于各种目的。一些注释的主要应用区域如下:

Comments are used for various purposes in C++. Some of the main areas of application of comments are given as follows:

  1. To represent a short and concise step in the program for users to understand better.

  2. To explain a step in a detailed way that is not expressed explicitly in the code.

  3. To leave different hints for users to grab in the code itself.

  4. To leave comments for fun or recreation.

  5. To temporarily disable part of the code for debugging purposes.

  6. To add metadata to the code for future purposes.

  7. To create documentation for the code, for example, in Github pages.