C++

C++ Comments

“The purpose of comments is to serve as a reminder to you and to let others know how your program works. Large text code explanations and code snippets that need to be commented out while debugging applications are both examples of multiline comments. The compiler doesn’t take comments into account. The comments in C++ can be one or more lines long. Any character that is present inside a comment is disregarded by the C++ compiler. Your comments should make it possible for outside users to understand your code.

If comments regarding program details are not supplied, someone reading a huge amount of code will be perplexed. Making a code more comprehensible by adding extra descriptions is possible with comments. To make the code understandable, comments may contain an algorithm description. If code needs to be reused after a significant break, comments can be useful for the individual as well. Comments shouldn’t be used as a stand-in for an English language explanation of badly written code. Writing clean, self-explanatory code should always be a priority. Next, use the comments section.”

Example 1

The single-line comment is represented by the double slash “//”. The single-line comments are indicated by using it. Only one line can be commented on by this. Given that it was designed to work with C++ programming, it is known as C++-style comments.

#include <iostream>
// Example of a single line comment
int main()
{
    // Main method body
    std::cout << "Single line comment in c++";
    return 0;
}

Here, we have first imported the iostream library with the C++ include keyword. After that, we used the double slash “//” and input a text line which demonstrates that this is a program of single-line comments. Entering the main body where we have also commented out the text line, which defines that the code is in the main method section.

The single-line comment is not displayed on the console screen.

Example 2

The /* any text line */ is the representation, with the beginning a forward slash and an asterisk (/*) and the ending an asterisk and a forward slash (*/). Multi-line comments are indicated by using it. More than one line may be affected by the comment. It was first used in C programming, hence the name “C-Style comment.”

/* Using a multi-line
comment is demonstrated
in a C++ program.*/

#include <iostream>
int main()
{
     
    /* Inside the multiline comment, the
    code is explained. The variable num is
    declared and printed on the screen*/

    int num = 25;
    std::cout << "The numbers is:";
    std::cout<<num<<"\n";
    return 0;
}

Here, we have started our program with the multiline comment. As discussed, the multiline comment used the single slash “/” with the asterisk “*” sign. We have wrapped our text paragraph in the multiline comment. The text gives the details that we have used the multiline comments in the C++ program. Then, we have to begin the program implementation by including the C++ module. After that, we introduced the main method, inside which we have again used the commented multiline text. The multiline comment explains that “num” is the variable name, and we have displayed the “num” variable value on the screen.

Upon the compilation of this C++ program, only the print statement is displayed on the prompt.

Example 3

Code can also be disabled in comments to stop it from running. For instance, instead of eliminating the problematic code when we encounter an issue when executing the program, we can use comments to prevent it from being executed; this can be a very helpful debugging tool.

#include <iostream>
using namespace std;
int main() {
   cout << "C++ Program\n";
   //cout << ''Error Program;
   cout << "C++ other Program";

   return 0;
}

In the above program, we have deliberately put an error command on the cout. Notice that in the second cout command, the string is outside the quotes, and we utilize the single quotes. This generates the exception.

Here is an example of how to utilize comments as a debugging tool. To avoid generating an error, we have just commented on the error command.

#include <iostream>
using namespace std;
int main() {
   cout << "C++ Program\n";
   //cout << ''Error Program;
   cout << "C++ other Program";

   return 0;
}

Now, you see that prompt has printed the cout statements rather than generated an exception.

Example 4

Block comments are a particular form of multiline comments that are used at the start of every file or function. The creator and purpose of the file are disclosed in these block remarks. How to create a block comment is as follows:

  • To keep the comment aligned with the code that follows it, the initial line is indented.
  • Information about the class’s or function’s intended use is provided in the comment.
  • Between the file’s description and a list of tags, there is a blank line in the comment.
/*
    CS-102 C++ 11, file.cpp
    Purpose: Documentation comments
 
    @author linux_user
    @version 3.3 16/07/2022
*/

#include <iostream>
using namespace std;
int main() {
 

   // cout<<"C++ eaxmple of Documentation comment"
   // print cout statement

   return 0;
}

Here, we have properly documented this program code with the blocked comments. You can see that we have given information about the code, which provides the author name, file details, version of the software, and also the date.

Example 5

A single-line comment may be nested inside of another comment that has several lines. To pairs of multi-line comments, nevertheless, this logic is inapplicable. There is no way to nest one multi-line comment inside another.

#include <iostream>
using namespace std;
int main() {
 /*
  // The single line comment
//Multiline comment start here, and the comment line ends here */

   return 0;
}

In this particular example, we have used the nested single-line comment with the multiline comment representation. We have wrapped our single-line comment with the single slash “/” and the asterisk “*”. This is how we can comment on several lines of text in C++.

Conclusion

Both single-line and multi-line C# comments were examined. Comments that are one line long come to an end at the first line break that comes after the // comment marker. Either put it after the code statement or put it at the top. It is considered to be a comment if it comes after a code statement. During compilation, only the text that is present between the comment delimiters will be recognized as the comment.

About the author

Omar Farooq

Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.