Comments in C++

Comments in C++ are special notes that we write inside a program to explain the code. They make the program easier to read and understand, especially when someone reads it later. The compiler completely ignores comments, so they never affect the program’s output.

Why Use Comments?

  • Improve Readability: Make the code clear and understandable for others or yourself.
  • Explain Logic: Helpful in describing what a part of the code is doing.
  • Temporarily Disable Code: Used for testing by stopping some lines from running without deleting them.
  • Documentation: Provide extra details about a program, function, or block of code.
  • Debugging: Comments help in error-finding by turning off some lines while keeping the original code safe.

Types of Comments

  1. Single-Line Comment

    In C++, a single-line comment begins with //. Anything written after // on that line is ignored by the compiler. It is used for writing short notes or explaining one line of code.

    Example:

    // This is a single-line comment
    #include <iostream>
    using namespace std;
    
    int main() {
        cout << "Hello, From ShikshaSanchar!" << endl; // Prints message
        return 0;
    }

    Output:

    Hello, From ShikshaSanchar!

    Explanation:

    • // This is a single-line comment is ignored by the compiler.
    • The comment after the cout statement explains what that line does.
    • These are best for short and quick explanations in code.
  2. Multi-Line Comment

    A multi-line comment starts with /* and ends with */. Everything between these symbols is ignored by the compiler. They are useful for detailed notes or stopping multiple lines of code from running.

    Example:

    /* This is a multi-line comment
       It can cover many lines
       and is ignored by the compiler */
    #include <iostream>
    using namespace std;
    
    int main() {
        cout << "Welcome to ShikshaSanchar C++ Course!" << endl;
        return 0;
    }

    Output:

    Welcome to ShikshaSanchar C++ Course!!

    Explanation:

    • Text between /* and */ is ignored completely.
    • They are useful when we want to write bigger notes or disable a block of code.

Best Practices for Writing Comments

  • Write short and meaningful comments.
  • Avoid writing unnecessary comments (like // prints hello above cout<<"hello";).
  • Update comments whenever code is changed.
  • Use comments to explain why something is written, not only what it does.
  • Prefer single-line comments, use multi-line only when required.

Example with Comments:

In this program, two predefined marks are added together. Comments are written to explain each step clearly.

Program:

// Program to calculate total marks
#include <iostream>
using namespace std;

int main() {
    int marks1 = 50;  // marks in first subject
    int marks2 = 40;  // marks in second subject

    // Adding the marks
    int total = marks1 + marks2;

    // Printing the result
    cout << "The total marks are: " << total << endl;

    return 0;
}

Output:

The total marks are: 90

Explanation:

  • marks1 and marks2 store the subject marks.
  • The + operator adds the two marks and saves it in total.
  • The cout statement displays the total marks on the screen.

Welcome to ShikshaSanchar!

ShikshaSanchar is a simple and helpful learning platform made for students who feel stressed by exams, assignments, or confusing topics. Here, you can study with clarity and confidence.

Here, learning is made simple. Notes are written in easy English, filled with clear theory, code examples, outputs, and real-life explanations — designed especially for students like you who want to understand, not just memorize.

Whether you’re from school, college, or someone learning out of curiosity — this site is for you. We’re here to help you in your exams, daily studies, and even to build a strong base for your future.

Each note on this platform is carefully prepared to suit all levels — beginner to advanced. You’ll find topics explained step by step, just like a good teacher would do in class. And the best part? You can study at your pace, anytime, anywhere.

Happy Learning! – Team ShikshaSanchar