In this guide, you will learn how to use comments in C programming language. We will discuss types of comments with examples in this tutorial.
What are Comments in C?
- Comment is a piece of text which doesn’t have any impact on the output of the program, it is there for documentation purposes.
- Comments are completely ignored by the C compilers.
- When you are working in an organization, you would want to include comments in your code so that the other programmer can understand the purpose of the code.
- This also helps maintaining an old project, where comments can be quite useful to understand the working of code that is written long back.
C Comments Example
In this example, we are putting a comment before the printf() statement to document the purpose of this print statement. This doesn’t change the outcome of the program.
#include <stdio.h>
int main() {
// Welcome message to BeginnersBook readers
printf("Hi, Welcome to BeginnersBook.com!");
return 0;
}
Output:
Hi, Welcome to BeginnersBook.com!
Types of Comments in C
1. Single line comment – Starts with //
and valid till the end of line
2. Multi-line comment – Starts with /*
and ends with */
, it can have any number of lines.
1. Single-line comments in C
In this example, we have three single line comments. These comments describe the purpose of C statements following the comment.
#include <stdio.h>
int main() {
// declaring and initializing two integer variables
int num1 = 100, num2 = 50;
// declaring another variable to store the sum
int sum = num1+num2;
// displaying the sum of two numbers
printf("%d",sum);
}
Output:
150
The single line comment can also be written like this: The comment after //
is ignored by the compiler and rest of the statement before //
is executed.
int sum = num1+num2;// declaring another variable to store the sum
2. Multi-line comments in C
When you want to write a long comment consists of multiple lines then you can start the comment with /*
and end the comment with */
. The whole block is ignored by compiler.
#include <stdio.h>
int main() {
int num=3;
/* Here we are finding out the square
* of the given number "num" by multiplying
* the number by itself and storing the result in
* another int variable numSquare
*/
int numSquare = num*num;
printf("%d",numSquare);
}
Output:
9
Use of comments in C
1. Improves readability: It makes the code easier to understand. Any programmer other the one who written the code, can understand the code by looking at the comments.
2. Look up a piece of code: You can search a piece of code by using the comment as a search term. For example, if you have written a code for some task “xyz” somewhere in a large C file, you can search the file using “xyz” to locate that piece of code.
3. Documentation: Comments help in documenting a code, which is a good programming practice. All the organisations always encourage documentation of codes.
4. Debugging: When we are debugging a code, instead of removing a piece of code to check for errors, you can simply comment out the code that you suspect is the cause of error and check again. If that doesn’t work you can un-comment the code and comment the other block of code and check again. This will help you debug the code faster without needing to make changes in the actual code.