?>
profile image
Announcements
No new announcements.
mentora icon
bookmark
Table of Contents

    Comments in C++ Programming and its Use

    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 are 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.

    Video Explanation

    Click the button below to watch the explanation.

    video image

    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 Comments - Multi-Line comments start with a slash asterisk /* and end with an asterisk slash */. It can occupy many lines of code. It is also known as Block Comments.

    Let's see some example for more understanding.

    Example 1 (Single-Line Comments)
    #include <iostream>
    using namespace std;
    
    int main()
    {
        // Printing Hello World on the screen.
        cout<<"Hello World";    // This line will print Hello World on the screen.
    
        return 0;
    }
    Output
    Hello World

    In the above program, we have used two single-line comments to explain the program. The first one is // Printing Hello World on the screen. and the second one is // This line will print Hello World on the screen.

    Example 2 (Multi-Line Comment)
    #include <iostream>
    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

    In the above program, we have used multi-line comments to explain what the program is all about. We can also comment out any statement that we don't want to execute in your program but want to keep it for future reference as shown above. We have commented out the statement //cout<<"\nThis is a C++ tutorial";.


    Mentora Question Asked: 0 / 20

    Hi, I’m Mentora!

    I’m your personal AI tutor from Dremendo. Ask me anything, and I’ll help you understand it in a simple and easy way.