C Programming

How to Write Comments in C Programming?

C programming is a robust language with versatile applications, ranging from operating systems to mobile applications. Like other programming languages, it is beneficial to write code that is easy to maintain and debug. Adding comments to your code is one way to accomplish this. Comments allow you to document and clarify your code, allowing other developers to easily understand and modify the code.

In this guide, we will go over the basics of writing comments in C programming.

What are Comments in C Programming?

Comments in C programming are a way to include non-executable text within the source code. They are crucial for programmers as they will help explain the functionality and purpose of their code to others. They provide critical information on the logic and reasoning behind specific code sequences, making it easier to develop and maintain code.

Types of Comments in C Programming

There are two types to write comments in C Programming:

1: Single-line Comments

One widely used method for writing comments in C Programming is using single-line comments. These comments start with “//” and end at the end of the line. They are typically used to clarify the purpose of a particular code statement that can be completed in a single line.

For example:

#include <stdio.h>

int main() {

  // create integer variable

  int num = 25;

  // print the age variable

  printf("Number: %d", num);

  return 0;

}

In the above program, the printf() function is used to print the value of the integer variable num, which is declared with the value 25. The program is explained through single-line comments.

Output

2: Multi-line Comments

To provide a comprehensive understanding of a larger section of code or complex algorithms, programmers can use block comments, which are also called multi-line comments. They begin with “/*” and end with “*/” and help explain lengthy comments that cannot fit into a single line.

For example:

#include <stdio.h>

/*This program initializes a

'num' variable and prints it

on the screen.*/


int main() {

  int num = 25;

  printf("Number: %d", num);

  return 0;

}

In the above program, the same code is used but the purpose of the program is explained initially using multi-line comments.

Measures to Take Before Writing Comments in C Programming

The comments should come before the code you want to describe, and they should explain what the code does, not how it does it. They should be concise, clear, and precise. Also, the comments should not repeat the code; instead, they should complement it.

In addition, it is essential to format your comments correctly. Write a single line of comment for a single line of code. If you write lengthy comments, wrap them to fit within 80 characters to make them easier to read. Writing clear and concise comments is crucial in making it effortless for fellow programmers to understand the code easily.

Conclusion

Writing comments in C Programming is a crucial step toward creating maintainable and readable code. With the use of single-line and multi-line comments, programmers can clarify the purpose and functionality of their code. It is also essential to follow certain guidelines when writing comments, such as being concise, clear, and precise, and formatting them correctly. By incorporating these practices, programmers can create more accessible and understandable code, which makes it easier for others to modify, debug, and maintain in the future.

About the author

Hiba Shafqat

I am a Computer Science student and a committed technical writer by choice. It is a great pleasure to share my knowledge with the world in which I have academic expertise.