Let's consider an example to demonstrate commenting in C Programming.
Example
#include <stdio.h> //header file
int main() //main body of the program
{
// A comment to provide information about below line
// printing a message to the console
printf("Hello! Welcome to the Logic Practice World"); //output statement
return 0; //return statement
}
Output:
Hello! Welcome to the Logic Practice World
Explanation:
In this instance, multiple comments have been included to simplify the explanation of the program. Comments beginning with (//) are non-compiled lines disregarded by the compiler.
Types of C Comments
There are primarily two varieties of comments in the C programming language.
- Single Line Comments
- Multi-Line Comments
Here, we will explore each category of comments individually.
Single Line Comments
In the C programming language, comments are denoted by double forward slashes (//) and are confined to a single line. These comments do not impact the program's execution, as the compiler disregards any text following //. Single-line comments are frequently employed for testing or debugging purposes to emphasize specific sections of code, offer concise clarifications, or temporarily deactivate specific lines of code.
C Single Line Example
Let's explore an illustration of a single-line comment in the C programming language.
Example
#include<stdio.h>
int main(){
//printing information
printf("Hello C");
return 0;
}
Output:
Hello C
Multi-Line Comments
In C programming, developers can provide detailed descriptions or explanations using multi-line comments in C++. These comments are denoted by the symbols slash asterisk \ ... \. They are capable of spanning across multiple lines of code, although nesting of these comments is not allowed.
Syntax
It has the following syntax:
/*
code
to be commented
*/
C Multi-Line Comments Example
Let's examine a demonstration of a multi-Line comment in C programming.
Example
#include <stdio.h>
int main(){
/*printing information
Multi-Line Comment*/
printf("Hello C");
return 0;
}
Output:
Hello C
Why are Comments used in C Language?
Any computer programming language, like C, is more concise than any human language, for example, English. It consists of far fewer keywords in comparison to any human language like English. C is considered to be one of the most compact languages, comprising only 32 keywords. As a result, comprehending the instructions within a C program can be challenging, especially for someone without a programming background.
The structure of the C programming language differs and can be intricate. Developers often strive to enhance efficiency by adding complexity to the code, which may, in turn, make it harder to understand. Comments play a crucial role in providing clear explanations and revealing the purpose behind employing a particular approach.
We should use comments because:
- Comments make our code easier to read in the future.
- Comments are used for debugging purposes.
- We can utilize comments for code collaboration because comments assist peer developers in comprehending our code.
Uses of comments in C
There are several uses of comments in C. Some of them are as follows:
- Enhances readability: It makes the code readable. Any other programmer, except the programmer who created the code, can comprehend the code by reading the comments.
- Search a piece of code: We can search a piece of code with the help of the comment as a search keyword. For instance, if we have written code for some operation "xyz" somewhere in a huge C file, we can search the file using "xyz" to find that piece of code.
- Documentation: Comments make documentation of a code easier, which is an excellent practice in programming. All organizations always promote the documentation of codes.
- Debugging: When debugging a code, rather than deleting a segment of code to test for errors, we can just comment out the code we think is causing an error and test again. If it fails, we can uncomment the code comment on the other segment of code and test again. It will help us debug the code more quickly without needing to modify the actual code.