C Comments
The comments in C are human-readable notes in the source code of a C program used to make the program easier to read and understand. They are not a part of the executable program by the compiler or an interpreter.
Example:
#include <stdio.h>
int main() {
// This is a comment, the below
// statement will not be executed
// printf("Hi from comment");
printf("Hello");
return 0;
}
Output
Hello
Types of Comments in C
In C, there are two types of comments in C language:
- Single-line Comments
- Multi-line Comments
Single-line Comments
Single-line comments are used to comment out a single line of code or a part of it. The single line comments in C start with two forward slashes (//), and everything after the slashes on that line is considered a comment. They are also called C++ Style comments as they were first introduced in C++ and later adopted in C also.
Syntax
// This is a single line comment
Example:
#include <stdio.h>
int main() {
// This is a single-line comment explaining the variable x
int x = 5;
// Output the value of x
printf("Value of x: %d\n", x);
return 0;
}
Output
Value of x: 5
In this C program, the comments provide explanations about the code. The compiler ignores the comments and does not execute them.
We can also create a comment that displays at the end of a line of code using a single-line comment. But generally, it's better to practice putting the comment before the line of code.
#include <stdio.h>
int main() {
// single line comment here
printf("Hi"); // After line comment here
return 0;
}
Output
Hi
Multi-line Comments
Multi-line comments in C are used write comments that span more than one line. They are generally used for longer descriptions or for commenting out multiple lines of code. In C language, these comments begin with /*
and end with */
. Everything between these markers is treated as a comment.
Syntax:
/* This is a multi-line comment
which can span multiple lines */
Example:
#include <stdio.h>
int main() {
/*
This comment contains some code which
will not be executed.
printf("Code enclosed in Comment");
*/
printf("Welcome to GeeksforGeeks");
return 0;
}
Output
Welcome to GeeksforGeeks
Nesting Comments in C
In C language, comments cannot be nested. That means you cannot place a multi-line comment inside another multi-line comment in C language. If you try to do so, the compiler will treat the closing */
of the inner comment as the end of the entire multi-line comment. and the rest of the part after the inner closing (*/) will not be commented.
#include <stdio.h>
int main() {
/* This is the start of a multi-line comment
/* This is an inner multi-line comment */
This line will cause an error because the compiler
considers the above '*/' as the end of the comment block.
*/
printf("This program will not compile.\n");
return 0;
}
Output
./Solution.c: In function 'main':
./Solution.c:6:8: error: unknown type name 'This'
This line will cause an error because the compiler
^
./Solution.c:6:18: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'will'
This line will cause an error because the compiler
^
If nested comments are needed, it’s best to use single-line comments or comment out individual parts.
#include <stdio.h>
int main() {
// /* This block of code is commented out
// int x = 10;
// printf("Value of x: %d\n", x);
// */
printf("Program runs without errors.\n");
return 0;
}
Output
Program runs without errors.
Best Practices for Writing Comments in C
Following are some best practices to write comments in your C program:
- Write comments that are easy to understand.
- Do not comment on every line unnecessarily and avoid writing obvious comments.
- Update comments regularly.
- Focus on explaining the intent behind the code rather than restating the obvious.