Comments in C++

A comment is a human-readable text in the source code that the compiler ignores. Any informative item that a programmer does not want to be executed might be inserted using comments. It could be to explain or make a piece of code more readable. It can also be used to prevent the execution of alternative code after the debugging process is complete.

Comments can be singled-lined or multi-lined.

Single Line Comments

  • Single-line comments are preceded by two forward slashes (/).
  • Any information after the slashes / on the same line will be disregarded (not performed) because it is unparsable.

A single-line comment is used in this example.

#include <iostream>
 
int main()
{
    // This is a single line comment
    std::cout << "Hello World";
    return 0;
}

Multi-line comments

  • A multi-line comment begins with /* and terminates with */.
  • The compiler will discard any information between /* and */.

A multi-line comment is used in this example.

#include <iostream>
 
int main()
{
    /* This is a
    multi-line
    comment */
 
    std::cout << "Hello World";
    return 0;
}
Shubhajna Rai
Shubhajna Rai

A Civil Engineering Graduate interested to share valuable information with the aspirants.

Leave a Reply

Your email address will not be published. Required fields are marked *

Get the latest updates on your inbox

Be the first to receive the latest updates from Codesdoc by signing up to our email subscription.

    StudentProjects.in