All About C++ Comments
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.
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 – /* */
Best-suited C++ courses for you
Learn C++ with these high-rated online courses
Single Line Comments in C++
In C++, any statement that starts with the symbols // becomes a single-line comment. For example:
//declare a variableint n;
//initialize the variable with a valuen = 7;
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
Multi-Line Comments
In C++, a comment can extend to multiple statements that begin with the symbols /* */. For example:
/* declare a variableto store the age of a candidate*/int age = 32;
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;
}
This is the correct output line 1correct output line 2
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
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