Commenting in C
Commenting in C
Commenting in the C language is used to give out the information about the lines of the code which are included. It is one of the things that is widely used for the documentation. They are the part of the code snippets which will directly be ignored by the compiler as they have no programmatical use in that particular application.
They are the type of concept that makes every programming code and application more readable and efficient to the programmer or a user and helps the programmer by explaining the logic behind the code.
It is said that a well documented program is a good practice for every developer and also makes the debugging more easier as anyone can find the commented section and what function it does.
It can be considered the sentence that only a programmer reads and the compiler does not, which is the explanation or the annotation of the main code. Neither the compiler nor the interpreter executes or reads the commented part, and it will directly be ignored by the computer.
Comments can be a detailed or precise description of the source code of a program or explanations.
There are two types of comments used in the C programming language.
- Single line comments
- Multi line comments
Single line comments: They are the type of comments that use double slash, represented as following “//” to comment on a specific part of any program or application.
E.g.:
#include <stdio.h> int main() { int num1, num2; //integer data type int sum, sub, mult, mod; //integer data type float div; //float data type printf("Input any two numbers separated by comma : ");//user input scanf("%d,%d", &num1, &num2); sum = num1 + num2; //addition of numbers sub = num1 - num2; //subtraction of numbers mult = num1 * num2; //multiplication of numbers div = (float)num1 / num2; //division of numbers printf("The sum of the given numbers : %d\n", sum); printf("The difference of the given numbers : %d\n", sub); printf("The product of the given numbers : %d\n", mult); printf("The quotient of the given numbers : %f\n", div); return 0; }
Output:
Input any two numbers separated by a comma: 2, 4 The sum of the given numbers: 6 The difference of the given numbers: -2 The product of the given numbers: 8 The quotient of the given numbers: 0.500000
Multi line comments: They are the type of comments that start and end with a symbol including a slash with an asterisk represented in the following manner “/*.... */”. This type of commenting slash can be placed anywhere within the code or application, and the sentences in between the slashes will be commented. It can be filled with any number of characters and any number of lines. It should be sure that the closing of multi line comments should be done correctly else the whole program will throw an error.
/*Comment starts continues continues ... Comment ends*/
E.g.:
#include<stdio.h> int main() { /* Print Message as an Output */ printf("Welcome to Tutorials and Examples\n"); printf("I love C tutorials"); return 0; /* this will give an output */ }
Output:
Welcome to Tutorials and Examples I love C tutorials
You can also create a comment that displays at the end of a line of code. But generally, it’s a better practice to put the comment before the line of code.
When are comments in programming necessary tools?
- If no comments regarding the program's features are given, a person reading a big code will be perplexed.
- Comments are a method to put additional details into a code to make it more understandable.
- If comments are not included in an extensive code base, the reader will be perplexed. To make code more intelligible, comments might describe an algorithm.
- If code is to be reused over a lengthy period of time, comments might be helpful for one as well.
- A good programmer who produces codes that are only understood by the machine is better than the one who generates codes that were only comprehended by the machine.
- As a result, including comments to your code is highly recommended as a good programming technique. Comments do not affect programming since they are overlooked by the compiler.
- Comments help the developer understand the logic/algorithm of the code if he revisits it after a long time.