Commenting Style in C++
While writing the code, one should always take care that the whole program should be explanatory. In this comments plays an important role.
This
makes our code more readable, easier to understand, and also very useful while debugging the code for an error.
C++ provides us two types of commenting styles:
- Single Line Comments
- Multiple Lines Comments
Let see both of them in details.
Single Line Comments:
Single line comment is represented by double forward slash "//". This is used to comment a single line only. Let's check this with a piece of code:
In the above code we have commented two individual lines, the first one and the one which is printing the message "Hello Geeks". So when the
above code is
compiled, compiler will ignore the lines having "//"and prints only single message.
The final result on execution will be as:
Multiple Line Comments:
Representation of multiple line comment start with forward slash and asterisk "/*" and end with asterisk and forward slash "*/". It can apply
comments
in more than a single line. Its syntax is as:
Let's also check this with an example code.
In the above code we have commented the lines which are printing Message 1 to 5. So while compilation, compiler will ignore these commented
lines and the final output
after execution will be
In this way, we can comment any part of our code and also put some small notes inside comments in the code to make the code more readable
and easily understandable.