C++ Comments

A C++ comment is a piece of descriptive text which explains some aspect of a program. The compiler totally ignores the program comment and are only intended for programmers or readers. There are two ways of writing comments.

  • // line comment
  • /* block comment */

The first one that is, the line comment, ignores everything from where the pair of slash sign(//) starts to the end of the line. The second one, the block comment, ignores everything between the start of the /* and to the end of the */. The second block comment may consist of a single line of comment or block of comment.

Syntax:

// comment
/* comment */

Example:

// This is a comment
/* This is a line of comment.
   It can be of two or more lines. */

A C++ program that shows the use of comment using // line comment:

#include<iostream.h>
int main()
{
cout<<"Hello World";  //Displays Hello World
return 0;
}

The output of the above program is:

Hello World

A C++ program that shows the use of comment using /* block comment */:

/* This is a program in C++
   that shows the example of
   block of comments */
#include<iostream.h>
int main()
{
cout<<"Second Program";
return 0;
}

The output of the above program is:

Second Program
SHARE C++ Comments

You may also like...

Leave a Reply

Your email address will not be published.

Share