C++ Comments and C-style Comment in C++
Video Tutorial
Overview
Comments are non-executable statements. They are not executed by the compiler or the interpreter. There are two types of comments in C++: single line and multi-line. We can use comments to explain function descriptions, parameters, etc. They make our code easy to understand not only for us when we revisit it after some time but also for everyone trying to understand the code.
Scope of the Article
- This article explains comments in C++.
- It covers the different types of comments such as single and multi-line comments in C++.
- It explains why we should use comments in our code.
What are Comments?
Comments are statements that are ignored by the compiler or the interpreter. In other words, comments are essentially non-executable statements.
They are used to describe and define how our code works so that users can understand it with ease. We can also say the code is for compilers and comments are for human understanding.
Types of Comments in C++
1. Single line comment:
These are represented by two slash characters: //. It is used when we have a one-line comment. All the text between // and the end of a line is ignored by the C++ compiler. We can make single-line comments in many lines. Let's try to understand more about single line comment from the syntax and example below.
Example
Output
Explanation From the above example, we can see the line of code after // does not make any difference to the program. The compiler simply ignores this line during execution.
Note: We can also add inline comments as shown in the above example.
2. Multi-line comments
These are represented by /* ....*/. It is used to have comments in more than one line. All the text between /* and */ is ignored by the compiler. Let's try to understand more about multi-line comment from the syntax and example below.
Syntax
Example
Output
Explanation From the above example, we can see the lines of code enclosed between /* and */ do not make any difference to the program. The compiler simply ignores these lines during execution.
C-style Comments in C++
C-style comments are multi-line comments which are used to comment on large blocks of text or code. All the text between /* and */ is ignored by the compiler.
The characters /* start a comment except when it is placed within a character constant, a string literal or a comment.
Note: C-style comments cannot be nested otherwise, it gives a compilation error.
C++ Style Comments
C++ style or single-line comments are used to comment on single lines of text. All the text of that line after // is ignored by the compiler. Let's understand more about C++ style comments.
Example
- The character "//" introduces a comment except when it is placed within a character constant, a string literal or a comment.
Let's see the example below to understand this better.
Output
- C++ style comments can be nested with another C++ style comment or C-style comment.
When and Why to use Comments in Programming
- Comments make our code easy to understand not only for us when we revisit it after some time but also for everyone trying to understand the code.
- It enables us to write detailed descriptions of the algorithms used.
- Comments are used to describe the contents of a file.
Example
- Comments are used to describe how and when to use a class as well as any additional considerations necessary to correctly use the class.
- Comments are used to describe the input and output parameters of a function as well as the purpose of the function and how to use it as you can see from the example below.
Example
Output
Explanation The above example implements a function to calculate the factorial of a number. It also uses multi-line comments to explain the working of the function and its input/output parameters.
- Comments are used in API's documentation to specify API parameters, response structure, methods, exceptions thrown, etc.
- It can be used to include resources such as logos, diagrams, URLs, etc in our code.
- We can use comments to creating a task list to be followed in the future. Such comments are called TODO comments.
Example
- Comments can be used for debugging. We can comment on certain code snippets in our program so that it is not executed in the final program. This is done to find the source of the error.
Note: Adding a lot of comments can make the code verbose.
Conclusion
- Comments are statements that are not executed by the compiler or the interpreter.
- There are two types of comments: single and multi-line.
- C-style comments or multi-line comments are used to comment on large blocks of text or code.
- C++ style comments or single-line comments are used to comment on single lines of text or code.
- They make our code easy to understand not only for us when we revisit it after some time but also for everyone trying to understand the code.