Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

C++ Comments

C++ Comments

Program comments are explanatory statements; you can include comments in C++ code, which improves the readability of the source code. All programming languages allow some form of comments.

C++ supports single-line and multi-line comments. All characters in a comment are ignored by the C++ compiler.

C++ comments generally come in two forms:

A comment starts with // and continues to the end of the line. For example:

Example

#include

<
iostream
using
namespace
std

;

int

main

(

)

{

// This is a comment

cout

<<

"

Hello World!

"

;

return

0

;

}

It can also be placed after a statement:

Example

#include

<
iostream
using
namespace
std

;

int

main

(

)

{

cout

<<

"

Hello World!

"

;

// Output Hello World!

return

0

;

}

When the code above is compiled, the compiler ignores // This is a comment and // Output Hello World!, and finally produces the following result:


Hello World!

A C++ comment starts with /* and ends with */. For example:

#include

<
iostream
using
namespace
std

;

int

main

(

)

{

/*

This is a comment

*/

/*

C++ comments can also * span lines

*/

cout

<<

"

Hello World!

"

;

return

0

;

}

Within /* and / comment, the // characters have no special meaning. Within a // comment, the / and */ characters also have no special meaning. Therefore, you can nest one type of comment inside another. For example: A comment for outputting Hello World: cout << "Hello World"; // Output Hello World

/*

A comment for outputting Hello World: cout << "Hello World"; // Output Hello World

*/