Detailed Explanation of C++ Language Comments

C++ comments are statements that are not executed by the compiler. Comments in C++ programming can be used to explain code, variables, methods, or classes. With comments, you can also hide program code.

There are two types of comments in C++:

  • Single-line comments

  • Multi-line comments

👇Click to receive👇
👉C Language Knowledge Resource Collection

C++ Single-line Comments

Single-line comments start with // (double slashes). Let’s look at an example of a single-line comment in C++.

#include <iostream>
using namespace std;
int main() {   int x = 11; // x is a variable   cout << x << "\n";   return 0;}

Output:

11

C++ Multi-line Comments

Multi-line comments in C++ are used to comment out multiple lines of code. They are surrounded by slashes and asterisks (/* ….. */). Let’s look at an example of a multi-line comment in C++.

#include <iostream>
using namespace std;
int main() {   /* declare and   print variable in C++. */   int x = 35;   cout << x << "\n";   return 0;}

Output:

35

Comments are very useful for providing explanations of the code, enhancing readability, and helping others understand the code. It is a good habit to develop good commenting practices while writing code.

Detailed Explanation of C++ Language Comments



 Popular Recommendations
  • C Language Tutorial – C Program to Print Number Triangle

  • C Language Algorithm – “Spiral Matrix” Algorithm Problem

  • C++ Tutorial – Detailed Explanation of C++ Language’s Continue Statement

Leave a Comment