All About C++ Comments

All About C++ Comments

2 mins read477 Views Comment
Updated on Jan 16, 2023 18:21 IST

Comments in C++ are useful for explaining any code of the programming language. The ultimate goal of these comments is to ensure that the code is comprehensible. It can also help in preventing execution while testing an alternative code.

2022_07_Understanding-Break-Statement-in-C-2.jpg

All programming languages allow the usage of comments that act as hints you can include in your source code to make your code easier to read and understand by humans. 

In this article, we are going to learn how to add comments to your C++ codes. We will be covering the following sections today:

What are Comments in C++?

C++ comments are explanatory statements that a programmer can add to their programs in order to make the code more readable and much easier to comprehend. Comments are ignored entirely by C++ compilers and do not affect the execution of the code in any manner. 

C++ supports two ways to add comments to a code: 

· Single Line Comments – //

· Multi-line Comments – /* */  

Single Line Comments in C++

In C++, any statement that starts with the symbols // becomes a single-line comment. For example:

 
//declare a variable
int n;
//initialize the variable with a value
n = 7;
Copy code

In the above-given example, we have used two single-line comments:

  • //declare a variable
  • //initialize the variable with a value

Alternatively, single-line comments can also be added as shown below:

 
int n; //declare a variable
n = 7; //initialize the variable with a value
Copy code
Understanding Break Statement in C++
Understanding Break Statement in C++
The break statement is loop control statement that is used for the terminating the loop. These are used in the situations where we do not have the idea about actual...read more
Understanding Break Statement in C++
Understanding Break Statement in C++
The break statement is loop control statement that is used for the terminating the loop. These are used in the situations where we do not have the idea about actual...read more
Tokens in C Programming
Tokens in C Programming
Tokens are the smallest unit in the C program. Every keyword or character or sequence of characters that you come across in C is a token. It is the smallest...read more

Multi-Line Comments

In C++, a comment can extend to multiple statements that begin with the symbols /* */. For example:

 
/* declare a variable
to store the age of a candidate
*/
int age = 32;
Copy code

This method can be used to write both multi-line as well as single-line comments. 

Explore C++ courses

Why Use Comments in C++?

Comments are useful tools for debugging

You can use C++ comments to disable the code and prevent it from being executed. This is particularly useful during code debugging – an error encounter while running a program can simply be disabled by making it a comment instead of removing it. 

In case we get an error while we are running the program, instead of removing the error-prone code, use comments to disable it from being executed; this can be a valuable debugging tool.

Let’s look at the following example:

 
#include <iostream>
using namespace std;
int main() {
cout << "This is the correct output line 1"<<endl;
//cout="" <<' 'incorrect output<<endl;
cout<<"" "correct output line 2";
return 0;
}
Copy code
 
This is the correct output line 1
correct output line 2
Copy code

Explain your code better with comments

As mentioned above, adding comments to your code will improve its readability thereby making it easier for your colleagues and fellow programmers to interpret the code. However, keep in mind that comments do not substitute poorly written codes. They are meant to enhance the already well-structured programs – so it’s a good practice to explain why you did something rather than how you did it.

Endnotes

Hope this article was helpful for you to understand how to add single-line and multi-line comments in your C++ codes and their need in any programming language. If you want to learn more about C++ and solidify your basics, you can explore our articles on the C++ programming language.

_______________

Recently completed any professional course/certification from the market? Tell us what you liked or disliked in the course for more curated content.

Click here to submit its review with Shiksha Online

About the Author

This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio