Dremendo Tag Line

Comments in C++ Programming and its Use

Input / Output in C++

In this lesson, we will learn what is a comment and how can we use it for better coding, with the help of some example.

What is Comments

In C++ program Comments are portions of the code ignored by the compiler which allow the user to make simple notes in the relevant areas of the source code.

Comments in C++ programming are used to provide information about lines of code. It is widely used for documenting code. There are 2 types of comments in C++ programming.

  • Single Line Comments - Single line comments starts with \\.
  • Multi-Line (Block) Comments - Multi-Line comments start with a slash asterisk /* and end with an asterisk slash */. It can occupy many lines of code.

Let's see some example for more understanding.

Example 1 (Single Line Comments)

#include <iostream>
#include <conio.h>

using namespace std;

int main()
{
    // Printing Hello World on the screen.
    cout<<"Hello World";  // Now we are going to print Hello World.
    return 0;
}

Output

Hello World

Here you can see that in line number 8 we have used a single line comments to explain what we are going to do on the next line. You can also write comment after the end of the statement as shown above in line number 9.

Example 2 (Multi-Line Comment)

#include <iostream>
#include <conio.h>

using namespace std;

int main()
{
    /* In this program we will learn how
       to print Hello World on the screen */
    cout<<"Hello World";
    // cout<<"\nThis is a C++ tutorial";
    return 0;
}

Output

Hello World

Here you can see that from line number 8 to line number 9 we have used multi-line comments to explain what the program is all about. You can also comment on any statement that you don't want to execute in your program but want to keep it for future purpose as shown above in line number 11.

video-poster

Test Your Knowledge


You Scored :

Percentage :