The Big Master

Comments

In C++, comments are used to add explanatory or descriptive remarks within...

In C++, comments are used to add explanatory or descriptive remarks within the source code that are ignored by the compiler. They serve as human-readable annotations to provide clarity, explanations, or documentation about the code. Comments are useful for improving code readability, making notes, and aiding in code maintenance.

Why use comments:

  1. Code Documentation
  2. Code Readability:
  3. Future Reference
  4. Debugging
  5. Code Review

Single-line comments:

A single-line comment can be placed at the end of the statement. The compiler will not read any statement after single-line comments.

int main() {
    // This is a single-line comment.
    cout << "Hello, World!" << endl; // This line will print "Hello, World!"

    // You can use single-line comments to explain code or add notes.
    // Remember that comments are not executed as part of the program.

    return 0;
}

int main() {
    // This is a single-line comment.
    cout << "Hello, World!" << endl; // This line will print "Hello, World!"
    // next statement will never be executed. cout << " Hello World2 " ;
    return 0;
}

 

Multiple line comments:

In C++, multi-line comments, also known as block comments, are used to provide comments that span multiple lines. They are useful for adding detailed explanations, documenting code, temporarily disabling blocks of code, or adding section headers. Multi-line comments begin with /* and end with */.

#include <iostream>

int main() {
    /* This is a multiple-line comment.
       It can span multiple lines and comment out
       a block of code or provide detailed explanations. */

    cout << "Hello, World!" << endl;  

    return 0;
}

int main() {
    /* This is a multiple-line comment.
       It can span multiple lines and comment out
       a block of code or provide detailed explanations. */

    cout << "Hello, World!" << endl;

    /* You can use multiple-line comments to disable
       portions of code for testing or debugging purposes.
       For example:
    
       int x = 10;
       int y = 20;
       // int sum = x + y; // This line is commented out.
    */

    return 0;
}

Multiple line comments can be used in a single line. 

int main() {
    /* This is a multiple-line comment.used in single line */
    cout << "Hello, World!" << endl;   
    return 0;
}