Comments in C language are used to annotate the code, making it more understandable and maintainable. They are ignored by the compiler and do not affect the execution of the program. There are two types of comments in C:
1. Single-Line Comments
2. Multi-Line Comments
Single-line comments start with // and continue to the end of the line. They are typically used for short explanations or notes about the code.
#include <stdio.h>
void main() {
// Print WAY TO CODE to the console
printf("WAY TO CODE\\n");
}
Multi-line comments start with /* and end with */. They can span multiple lines and are useful for longer descriptions or to comment out large sections of code.
#include <stdio.h>
void main() {
/*
* This is a multi-line comment.
* It spans multiple lines.
* It is useful for detailed explanations or for commenting out blocks of
code.
*/
printf("way to code");
}
• Single-Line Comments: Use these for brief, concise notes about specific lines of code.
• Multi-Line Comments: Use these for longer explanations, descriptions of complex logic, or for temporarily disabling a block of code.
• Keep comments clear and concise.
• Avoid over-commenting; only comment on complex or non-obvious parts of the code.
• Use comments to explain the "why" behind the code, not the "what" (which should be clear from the code itself).