COMMENT LINE IN C
A good programming or development practice is to have well-documented programs or code. It improves a program's readability, maintainability, and ease of error detection. Code comments are an essential component of effective documentation. Consequently, we shall examine the C program comments in this tutorial.
Importance of a comment line
The program's source code is explained or described in a comment. It increases program readability and helps developers explain the code's meaning. A comment is not taken into account by the compiler during runtime.
A comment can be found anywhere within your program and is represented within the asterisk slash characters /* and */. The comments in your C code can cover many lines. Normally, comments are added above the C source code. You should include source code comments in your C code source. Adding too many comments to C source code is preferred instead of not writing any comments.
Syntax:
//Write your comment here
(Or)
/*Write your Comment Here*/
Types of Comments in C language
- Single line comments-//
- Multi-line comments-/* */
Single line comments (//) in C
- A single-line comment's first and last characters are two double-forward slashes (//).
- The compiler will not look at anything on the current line next to these forward slashes.
- Any text, including another double slash, may be included in a comment of this type. It works well when we only need to explain the code briefly.
Single-line comment example:
Code:
#include <stdio.h>
int main() {
// Single-line Comment
printf("Hello visitors! How are you all?");
return 0;
}
Output:
![COMMENT LINE IN C](https://static.tutorialandexample.com/c/comment-line-in-c1.png)
You can observe from the output that comments given through double forward slash are ignored or not displayed.
Multi-line comments in C
- The two delimiters for multi-line comments are /* and */, represented by a slash and an asterisk, respectively.
- Asterisk slash next */ marks the beginning and end of multi-line comments.
- Any character, including a newline, is not a */ may be used in these comments.
- The compiler treats whatever is between /* and */as a comment.
- We use this comment when explaining the code in detail or when more than one line is needed.
Let’s see an example of a multi-line comment:
Code:
#include <stdio.h>
int main() {
/*
It is a Multi-line comment in C
Printing the written characters in the print function
*/
printf("Do you like this article?");
return 0;
}
Output:
![COMMENT LINE IN C](https://static.tutorialandexample.com/c/comment-line-in-c2.png)
End of code line comments
In a program, the comments can be included at the end of the code line. You may add comments on text after a line ends.
Code:
int p=86; //Assigning integer 86 to the variable ‘p’
int p=86; /* Assigning integer 86 to the variable ‘p’ */
Guidelines for writing Comments:
- A false comment is harmful since it might mislead the person who reads it and cause compilation issues. Therefore, there are some guidelines for comment writing.
- Make careful about updating the comments once we make any code changes.
- Avoid using comment nesting, which prevents comment pairs from appearing inside one another.
- We can divide the comments.
- Any amount of comments can be written anywhere inside a single application.
What’s the use of comments?
- Using comments, we may make our program simpler to read and accessible when it gets lengthy.
- Comments summarize algorithms, specify a variable's function, or clarify a confusing code section.
- Including comments to illustrate how the code works is highly beneficial when we know that other programmers will also read our software.
- When code or a program is repeated after a long time, a comment supports self-help since it quickly summarizes all the code information.
Benefits of Using Comments in the Program
- A program seems simple to comprehend and operate when comments are used in the program.
- The program gained recognition because of its readability and maintainability through using comments that explain the logic and workings of the program.
- You can now understand your code thanks to comments, not just for other people but also for yourself.
Conclusion
Using comments in their code is a habit of a good programmer.
The code we write for our program in C is explained via comments.
Using comments, we may improve our code's readability, maintainability, and ease of error detection.
One form of commenting style supported by C is the single-line remark, while the other is the multi-line comment.
At the stage of the compilation process, the compiler ignores the commented content.