In programming, comments are hints that a programmer can add to make their code easier to read and understand. For example,
#include <stdio.h>
int main() {
// print Hello World to the screen
printf("Hello World");
return 0;
}
Output
Hello World
Here, // print Hello World to the screen
is a comment in C programming. Comments are completely ignored by C compilers.
Types of Comments
There are two ways to add comments in C:
//
- Single Line Comment/*...*/
- Multi-line Comment
1. Single-line Comments in C
In C, a single line comment starts with //
. It starts and ends in the same line. For example,
#include <stdio.h>
int main() {
// create integer variable
int age = 25;
// print the age variable
printf("Age: %d", age);
return 0;
}
Output
Age: 25
In the above example, // create integer variable
and // print the age variable
are two single line comments.
We can also use the single line comment along with the code. For example,
int age = 25; // create integer variable
Here, code before //
are executed and code after //
are ignored by the compiler.
2. Multi-line Comments in C
In C programming, there is another type of comment that allows us to comment on multiple lines at once, they are multi-line comments.
To write multi-line comments, we use the /*....*/
symbol. For example,
/* This program takes age input from the user
It stores it in the age variable
And, print the value using printf() */
#include <stdio.h>
int main() {
int age;
printf("Enter the age: ");
scanf("%d", &age);
printf("Age = %d", age);
return 0;
}
Output
Enter the age: 24 Age = 24
In this type of comment, the C compiler ignores everything from /*
to */.
Note: Remember the keyboard shortcut to use comments:
- Single Line comment: ctrl + / (windows) and cmd + / (mac)
- Multi line comment: ctrl + shift + / (windows) and cmd + shift + / (mac)
Use of Comments in C
1. Make Code Easier to Understand
If we write comments on our code, it will be easier to understand the code in the future. Otherwise you will end up spending a lot of time looking at our own code and trying to understand it.
Comments are even more important if you are working in a group. It makes it easier for other developers to understand and use your code.
2. Using Comments for debugging
While debugging there might be situations where we don't want some part of the code. For example,
In the program below, suppose we don't need data related to height. So, instead of removing the code related to height, we can simply convert them into comments.
// Program to take age and height input
#include <stdio.h>
int main() {
int age;
// double height;
printf("Enter the age: ");
scanf("%d", &age);
// printf("Enter the height: ");
// scanf("%lf", &height);
printf("Age = %d", age);
// printf("\nHeight = %.1lf", height);
return 0;
}
Now later on, if we need height again, all you need to do is remove the forward slashes. And, they will now become statements not comments.
Note: Comments are not and should not be used as a substitute to explain poorly written code. Always try to write clean, understandable code, and then use comments as an addition.
In most cases, always use comments to explain 'why' rather than 'how' and you are good to go.