What is comment in C?
In C, a comment is a piece of text in code that is not executed when the program is executed.
Comments are used to provide explanations, and keep documentation, or notes within the code for future reference and code understanding.
C supports two types of comments: single-line comments and multi-line comments.
Adding single-line comments in C
Just add two forward slashes (//
) before a line and the line will be treated as comment.
xxxxxxxxxx
// This is an example of single line comment
In the following example, two forward slashes have been added before the line 'This is an example of a single-line comment'
. C compiler will ignore the line as comment and it will not have any effect on the execution of the program or on output.
Again, two forward slashes have been added before the line 'This is another example of comment'
which is at the end of the printf() function. This is another way of adding comment at the end of C code.
Please take a closer look at the following example:
Example of C Single Line Comment
xxxxxxxxxx
#include <stdio.h>
int main() {
// This is an example of single line comment
printf("Add two forward slashes (//) to comment a single line"); // This is another example of comment
return 0;
}
Output of the above example
Add two forward slashes (//) to comment a single line
The output does not contain the commented lines.