In this section, we will learn what the comments are and how to use them in C.
What are comments in C?
Comments are a way of telling your opinion about a subject.
There might be an article over the Internet and you find it vague, so you put a comment down below the article so that the author gets to see your opinion and perhaps will fix the problem (if any) afterwards.
Or you might be in a room where a few people are having a conversation and one of the involved invites you to the conversation so as to get your opinion or, in another word, comments.
Basically, we have comments everywhere!
- In our day-to-day conversation with our friend.
- Over the Internet, like in the Twitter or other social networks down below a post of a friend, a celebrity, etc.
- In a meeting about a subject.
And other places.
Basically, comments can be helpful in order to open the details of a subject, shape and comb the subject if possible and of course as a result will give a chance to other people to speak their mind which helps everyone to understand the subject even better.
In C language, you can use comments between the lines of code too!
Let’s see an example here:
#include <stdio.h> int sum(int firstValue , int secondValue); int main() { int result = sum(3, 10); printf("resut: %d", result); return 0; } int sum(int firstValue , int secondValue){ return firstValue + secondValue; }
Even though this is a simple C program, for someone who doesn’t know C Programming, this can be confusing.
If there’s a tutor and he/ she is trying to explain this source code, she has a couple of choices here:
- She can bring out each line of code and explain them one by one (which is not a bad idea)
- Another choice that she can make is to use the power of comments and explain each line of code right in the source code itself.
Let’s take a look at the code again, but this time with the combination of comments:
/* #include is a preprocessor-directive and is used to replace the file named 'stdio.h' in this example with the actual content of that file. */ #include <stdio.h> //This is a prototype of a function and is necessary in order to tell the compiler that there's a function named sum. // the body of this function will be somewhere in the source code and it's the job of the compiler to find the body // of this function... int sum(int firstValue , int secondValue); // This is the main function and the execution of your program starts from here. int main() { int result = sum(/* the first value is an integer*/3, /* the second value is also an integer*/10); // We are calling another function that its prototype exists in the stdio.h file. printf("resut: %d", result); // the return type here is an integer and since the value is 0 it means everything ended well. return 0; } /* this is the body of the prototype function named 'sum' */ int sum/* Each variable that we have in the parentheses of a function * is called "parameter" */(int /* a comment can be put anywhere and * they are ignored by the compilers */firstValue , int secondValue){ return firstValue + secondValue; }
If you run the program above, the code will run without any problem even though we have mixed it with comments.
In short, comments help us to read and make sense of a program better.
There are lots of use cases for comments in programming! For example, let’s say you have a source code to check and see if there’s a problem with it. Now, if you found a problem in the program, you can simply write a comment on top of the section where you think the problem exists and give your opinion about the bug. Now you can pass the code back to the developer whom wrote the code and she can see the exact location and the reason of an error in order to fix it.
Note that comments are for developers and not the C compilers! Basically, compilers will remove the comments from a source code first and then continue to compile and run the program.
Types of C Comments: How to comment in C?
There are two types of comment in C program:
- Single line commenting.
- Multiple line commenting.
Single Line Comment in C
In order to create a single line comment, we start with two forward-slashes (with no space in between) and then put our comment right afterward.
For example:
// this is a single line comment.
In this way just that line is considered as comment and of course you can no-longer use that line for writing code because the entire line after `//` is considered as part of the comment even if you don’t fill that line with any text other than `//`.
You should know that compilers ignore comments altogether and that’s why you can’t use the commented section of your program to write code and expect it to run by the compiler! The ignoring reason is because comments are used to help developers to better understand their program.
Compilers don’t need comments and so they skip over it!
Also, in a comment you can write anything! Even a joke!!! But please don’t!
Basically, we use comments to explain the details of the logic we have used in part or the entire source code that we have worked on so in later times we or other developers can read these comments and figure out what changes should be done now (if any) and how.
Example: creating single line comment in C
#include <stdio.h> //This is a single line comment... int main() { return 0; }
Multi Line Comment in C
As the name suggests, this type of commenting is not limited to just one line and we can expand our comment to multiple lines.
Note: no-matter how many lines a comment takes; they all will be ignored by the compilers.
Here’s the structure of a multi-line comment:
/* body of the comment */
Note: there’s no space between forward-slash and asterisk that comes before and after comments.
A multi-line comment starts with a forward-slash followed by an asterisk followed by the actual comment, which can be one or multiple lines of comments and then followed by another asterisk and ends with another forward-slash.
As the last note of this section, you should know that we can use comments in any section of our source code except:
- Between the names of keywords and variables and functions.
For example:
ret/*a comment between the 'return' keyword! */urn 0;
This is illegal and the compiler will return error because `return` is a C language keyword and we broke it by adding comments in between.
Example: creating multi line comment in C
#include <stdio.h> /* This is a multi line comment ..... */ int main() { return 0; }