Comments are notes you leave in your code for yourself and other programmers. The compiler ignores them completely. They are for humans, not machines.
Good comments explain why you did something, not what you did. The code itself should show what it does. Use comments to explain the reasoning behind a tricky decision or to remind yourself of something important.
Single-Line Comments
Use // for a single-line comment. Everything after // on that line is ignored.
int score = 100; // This is a single-line comment
Multi-Line Comments
Use /* */ for multi-line comments. Everything between the opening /* and closing */ is a comment, even if it spans multiple lines.
/* This is a multi-line comment.
It can span several lines.
Everything here is ignored by the compiler. */
Why Comments Matter
You will thank yourself later. Six months from now, when you come back to a program you wrote, comments will save you hours of re-reading code trying to understand what you were thinking. Write comments as if the person reading them is a violent stranger who knows where you live. Be clear. Be kind.
#include
int main() {
// Ask the user for their favorite number
int number;
printf("Enter your favorite number: ");
scanf("%d", &number);
/* Double it and show the result.
This is just for fun. */
printf("Double that is: %d\n", number * 2);
return 0;
}
Try it Yourself โ