C++ Comment


A comment is text that the compiler ignores but that is useful for programmers.

Comments are normally used to annotate code for future reference.

The compiler treats them as white space. You can use comments in testing to make certain lines of code inactive; however, #if / #endif preprocessor directives work better for this because you can surround code that contains comments but you cannot nest comments.

A C++ comment is written in one of the following ways:

  • The /* (slash, asterisk) characters, followed by any sequence of characters (including new lines), followed by the */ characters. This syntax is the same as ANSI C.
  • The // (two slashes) characters, followed by any sequence of characters. A new line not immediately preceded by a backslash terminates this form of comment. Therefore, it is commonly called a single-line comment.

The comment characters (/*, */, and //) have no special meaning within a character constant, string literal, or comment.

Example

#include <iostream>
using namespace std;
// main() is where program execution begins.
int main()
{
cout <<"Hello World"; // prints Hello World
return0;
}
Output
Hello World

Share this article on