C Programming Language Comments
Comments In C Language , comments serve as valuable annotations that programmers can incorporate to enhance the readability and comprehension of their code. For instance, consider the
following code snippet in C:#include <stdio.h> int main() { // print Hello World to the screen printf("Hello World"); return 0; }
When we encounter “// print Hello World to the screen” in C programming, it signifies a comment. C compilers completely disregard comments.
Types of Comments in C
In C, two methods exist for adding comments:
- Single-Line Comments
- Multi-Line Comments
- Single-Line Comments
In C, single-line comments commence with “//” and conclude within the same line. For instance:
#include <stdio.h>
int main() {
// create integer variable
int age = 25;
// print the age variable
printf("Age: %d", age);
return 0;
}
In the above example, “// create integer variable” and “// print the age variable” are two single-line comments. Single-line comments can also accompany code, such as:
int age = 25; // create integer variable
In this case, code before “//” is executed, while code after “//” is ignored by the compiler.
- Multi-Line Comments
C programming provides a different type of comment that allows multiple lines to be commented simultaneously. These are known as multi-line comments. To create multi-line comments, we use the “/* … */” symbols. For instance:
/* This program takes age input from the user
It stores it in the age variable
And, prints the value using printf() */
#include <stdio.h>
int main() {
int age;
printf("Enter the age: ");
scanf("%d", &age);
printf("Age = %d", age);
return 0;
}
Within multi-line comments, everything between “/” and “/” is disregarded by the C compiler.
To utilize comments efficiently, remember these keyboard shortcuts:
- Single-Line comment: ctrl + / (Windows) and cmd + / (Mac)
- Multi-line comment: ctrl + shift + / (Windows) and cmd + shift + / (Mac)
Utilizing Comments in C
- Enhancing Code Comprehension
Incorporating comments into our code significantly improves its comprehensibility. Without comments, deciphering one’s own code can be time-consuming. Moreover, when working in a group, comments facilitate other developers’ understanding and utilization of your code.
- Debugging with Comments
During debugging, there may be instances when specific sections of the code are not required. For instance:
// 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;
}
In this example, if we decide we no longer need the height data, we can simply convert the corresponding code into comments. Subsequently, if we require height data again, removing the forward slashes will transform them back into statements rather than comments.
Discover more from PiEmbSysTech
Subscribe to get the latest posts sent to your email.