Comment In C++ | Types, Usage, C-Style Comments & More (+Examples)
Comments are texts written in a program along with the code to provide descriptions and key points about what’s happening in the code. These comments are mostly supported in all programming languages to make the code readable. Comments in C++ are mostly of two types- single-line comments and multi-line comments in C++. It helps programmers to understand the code better and check execution for alternative codes as well.
In this article, we will have a look at different types of comments in C++, how to comment in C++, c style comments in C++ programming, the use of comments for debugging, and how the compiler processes C++ comments. We will see various examples along with the explanation to better understand the topic.
Types of Comments in C++
As mentioned before, we mostly use two types of comments to make our code more readable and understandable in C++ language. They are:
- Single Line Comments
- Multi-Line Comments
We will discuss both of them one by one in this article. So, let’s get started.
Single Line Comment In C++
As the name suggests, a C++ single-line comment is a comment that consists of one line only. That is, it can occupy only a single line of the code. These comments are represented by two forward slashes(//).
Anything written after // until the end of the first line is a single-line comment. These comments are only for the programmers to understand the code better. The compiler ignores everything written after //, and hence the comments do not affect the program directly in any way.
Syntax of Single line comment in C++
// declaring a string s
string s;// initializing the string 's' as “unstop”
s= “unstop”;
Here, the single-line comments are used to mention what we are doing with strings. That is,
- // declaring a string s
- // initializing the string 's' as “unstop”
Let's take a look at a code example to see single-line comments in action.
Code Example:
Output:
Single-line comment in C++
Explanation:
In the example here,
- We begin by including the iostream file.
- Next, in the main function, we add a single-line comment beginning with //.
- And then, we use the cout command to print a statement.
- The output shows that only the cout command is implemented while the content written after // goes unseen.
The above code snippet demonstrates the use of single-line comments in C++. These comments are normally unseen/ ignored by the compiler while the rest of the code gets executed. In the above example, the program simply prints a single-line comment in C++ as output.
Multi-Line Comment In C++
C++ also supports multi-line comments, which are spread over many lines to provide a description of the code. That is, these comments can span multiple lines in a code. They are enclosed in the delimiters /*.....*/.
That is, anything written between /* until the end of the */ is a multi-line comment. Just like single-line comments, these comments are also only for the programmers to understand the code better. And again, the compiler ignores everything written between /*..*/, and hence the comments do not affect the program directly in any way.
Syntax of multi-line comment in C++
/*
C++
Multi
Line
Comment
*/
Code Example:
Output:
Multi Line Comment in C++
Explanation:
In the code example, we begin by-
- The iostream file and also use the namespace std.
- We then leave a comment enclosed in /*...*/ in the main int function.
- After that, we use the cout command to print output and close the program with return 0.
- As seen in the output window, only the cout command is executed, and the content within the /*...*/ delimiters is unprocessed.
The code above demonstrates the use of multi-line comments in C++. Again the comments go unseen by the compiler while it executes the rest of the code. In the above example, the program simply prints multi-line comment in C++ as output.
Also read: Pointers in C++ | A Roadmap To All Types Of Pointers With Examples
How Do Compilers Process Comments In C++?
The compilation process consists of four primary stages, i.e., the pre-processor stage, compiling, assembling, and linking. The compiler carries out lexical analysis, syntax analysis, and cod generations within the compiling stage.
The lexical analysis stage is also known as the scanning or tokenization phase, during which the compiler identifies the different elements/ tokens of the code, such as identifiers, operators, literals, etc. During this stage, the compiler would ideally decide what to do with the comments. But it instead treats them as whitespace and skips them. In other words, the compiler ignores the comments during the compilation process.
In other words, the comments are not a part of the compiled output, and the only primary function they serve is to provide an explanation of the code without affecting its functionality. So, at the end of the C++ program, there are no comments left, and the program is compiled successfully. This proves that comments are only meant for programmers to understand things and not for the machine itself.
Code Example-
Output:
Demonstrating compiler process.
Explanation:
As shown in the code example above,
- Beginning with the first line, the compiler ignores the first single-line comment during the compilation process and moves ahead to include the iostream file and the namespace std.
- Within the main function, it ignores the multi-line comment and goes directly to the cout command.
- It hence prints “Demonstrating compiler process”.
- Finally, the function then returns 0, and the compilation finishes.
Check this out- Boosting Career Opportunities For Engineers Through E-School Competitions
C- Style Comments In C++
C-style comment methods are for large blocks of text or code and are popularly known as multi-line comments. However, they can be used to comment on single lines as well. We can simply write texts between /* … */ to indicate it as a C-style comment. The compiler ignores all the contents between /* … */during compilation.
In the case of C++-style comments, they are usually used to comment on single lines of text or code and are popularly known as single-line comments. However, they can be merged together to form multi-line comments. We can simply write texts starting with // to indicate it as C++-style comments. Everything written between // until the start of the new line is ignored by the compiler during compilation.
The syntax for writing C-style comments within C++-style comments:
// int a=5; /*declare a integer variable a as 5 */
Here,
// int a=5; is a C++ style comment and,
/*declare a integer variable a as 5 */ is a C style comment.
Code Example:
Output:
C style comments in C++
Explanation:
The above code snippets explain the difference between C-style comments and C++-style comments. It denotes the syntax in which it can be written in a code segment. During compilation, the compiler ignores both of them, and the program simply prints “C style comments in C++” as output.
How To Use Comment In C++ For Debugging Purposes?
One of the most important uses of program comments is to debug programs in code editors. Whenever we come across errors in a code, we start the debugging process by commenting out some segments of error-prone code. For this, we can use the delimiters /*..*/ to check whether the respective part of the code is causing an error. This helps us save time and helps avoid deleting/ rewriting the entire code.
Also, even if the code turns out to be right, we can move ahead with some other segment of code and continue until we find the error-causing code. This is an efficient way to figure out errors and help in debugging the code. The syntax for this is the same as a multiline comment in C++.
Syntax:
/* int x=5, y=6, mult=0;
mult= x*y;
cout << “multiply=” << mult; */
Code Example (Without Comments):
Output:
Compilation error
missing ; before cout.
Here, we get a compilation error because a (;) is missing from one of the lines. While this is a short code and one can browse through it. The same might not be feasible in the case of comparatively complex codes.
Debugging the code using comment-
Output:
Sum=11
Explanation:
In the above code example, the program first displays an error while compiling it for the first time. To figure out the error, we can convert the second half of the code into a comment and see if the rest of the code works.
So, we comment out the later section of the code and then compile it again. This time the code executes and gives the output as Sum = 11. It helps us understand that the problem is with that latter part where a ; missing. With the scope of error narrowed down, we can easily rectify our code and compile it again to get the output.
Final Code after debugging-
Output:
Sum=11
Difference=1
Note: After successfully debugging the code, we can rectify the errors and arrive at the final solution. Now, the code snippets evaluate the sum and difference of 2 numbers and print it. The function then returns 0, and the compilation ends.
When To Use Comments While Writing Codes?
There are three primary cases in which we can use comments in codes. They are:
Case 1: At the beginning of the program, to mention the aim of the program and what the program does.
Case 2: At the beginning of each function, to write the description of the function and what the function returns.
Case 3: In case of complex lines of code, to understand the logic and help debug the code.
Why Do We Use Comments In Codes?
Comments in programming are used to provide well-structured and self-explanatory code. They are written in human-readable language and are ignored by the compiler or interpreter, meaning they don't affect the execution of the program. Comments are primarily used for the following reasons:
- It makes the code more readable.
- It can help summarize the entire code and what the program does in explanatory statements.
- While working in a group, individuals' commenting functions help fellow programmers understand what's happening.
- It helps in understanding the difficult segments of a code by providing a brief description.
- It makes finding bugs easier by debugging the program step-by-step.
- It makes commenting on alternative approaches to the solution much easier and more understandable.
- It helps save time by identifying important blocks of code easily.
- It makes the task of going through past codes much easier, and the maintenance of code is also done nicely.
Conclusion
In this article, we discussed comments in C++ and its different types with the help of examples. We also discussed how it is a part of good programming practices and helps one write more readable code. We now know that the compiler simply ignores the comment and focuses only on the code. It is also evident that comments can play a crucial role in debugging and making a program functional. As discussed in the article, there are multiple reasons for using comments, which is why everyone must know how to use them properly.
Frequently Asked Questions
Q. What are the comments in C++?
Comments are texts written in a program along with the code to provide a description and key points about what’s happening in the code. These comments are mostly supported in all programming languages to make the code readable. Comments in C++ have mainly two types, i.e., single-line and multi-line comments in C++. It helps programmers to understand the code better and check execution for alternative codes as well.
Q. Does C++ compile comments?
The answer to this is no. The compiler skips over the comments and does not include them as a part of the lexical analysis in the tokenization phase. Since the comments do not add any functionality or affect the execution of the code in any way, they are simply ignored in the compilation process. This is also because comments are only for programmers to either understand the code better, for future references, or for debugging, etc.
Q. What types of comments are supported by C++?
Comments are mostly supported in all programming languages to make the code readable. Comments in C++ are mostly of two types:
1. Single-line comments in C++, i.e., comments that span only one code line. These comments begin with double slashes (//), and the syntax for these is-
//C++ single-line comment
2. Multi-line comments in C++, i.e., comments that can span multiple lines of code. These comments are enclosed in /*...*/ delimiters. The syntax hence is:
/*
C++
Multi
Line
Comment
*/
These comments help programmers understand the code, debug it, make it more readable, and help check execution for alternative codes.
Q. How do you comment on multiple lines?
To begin with, note that a multi-line comment is a single comment which spans multiple lines of code. So we can directly use these types of comments to comment on multiple lines. But this is not the only way to do so. In fact, we can also use single-line comments to comment on multiple lines. Let's see how that is done.
Using single-line comments:
When using single-line comments, we have to comment on every line we want individually. And as usual, the comment needs to start with a double forward slash (//).
Example:
Output:
Using single-line comment
Using multi-line comments:
Multi-line comments in C++, by definition, allow us to comment on multiple lines. So all we have to do is enclose our comments in the /*..*/ delimiters. An example of the same is given below:
Example:
Output:
Commenting on line1
Using single-line comment
Q. What is the use of /* */?
Multi-line comments are used in C++ to write large text descriptions and explain the code to make it more user-friendly. It can also be used to mark different alternative approaches to code as comments which can be useful in the future to fellow programmers. All of these blocks of comments will be simply ignored by the compiler while compilation of the program.
We have reached the end of the article on comments in C++. You might also be interested in reading:
- References In C++ | Declare, Types, Properties & More (+Examples)
- Dynamic Memory Allocation In C++ Explained In Detail (With Examples)
- 51 C++ Interview Questions For Freshers & Experienced (With Answers)
- Typedef In C++ | Syntax, Application & How To Use It (With Examples)
- Difference Between C And C++| Features | Application & More!
Login to continue reading
And access exclusive content, personalized recommendations, and career-boosting opportunities.
Comments
Add comment