C Comments
Comments are notes in your code that the compiler ignores. They are used to explain what code does, making it easier for you and others to understand later.
C supports single-line comments starting with //, and multi-line comments enclosed between /* and */.
// single-line comment
/* multi-line
comment */Single-line comments
Anything after // on a line is ignored by the compiler. These are great for short explanations next to a line of code.
Multi-line comments
Text between /* and */ can span multiple lines and is often used for longer explanations or temporarily disabling blocks of code.
#include <stdio.h>
int main() {
// This prints a greeting
printf("Hello!\n");
return 0;
}Hello!The single-line comment is ignored during compilation.
#include <stdio.h>
/* This program
prints a number */
int main() {
printf("%d\n", 42);
return 0;
}42The multi-line comment describes the program above main().
Key points
- // starts a single-line comment.
- /* ... */ wraps a multi-line comment.
- Comments are ignored by the compiler.
- Good comments explain why, not just what, the code does.
