Comments in C Language

In C language, comments are used to add notes and descriptions to the code that explain its functionality. These comments are not compiled, so they do not affect the program's execution but help the programmer understand the code.

There are two types of comments in C language:

  • Single Line Comments
  • Multi-Line Comments

Single-line Comments

Single-line comments begin with two forward slashes (//) and extend to the end of the line. The compiler ignores everything after the double slashes. Here is an example of a single-line comment:

Multi-line Comments

Multi-line comments, also called block comments, begin with a forward slash and an asterisk /* and end with an asterisk and a forward slash */. These comments can span multiple lines. Here is an example of a multi-line comment:

Comments are useful for explaining the code and making it more readable. They can also be used to temporarily disable parts of the code during testing or debugging.

Here's an example of how comments can be used to explain the code:

Output:

Single or multi-line comments?

It is depend upon situation which one you want to use. Usually, we use // for short comments and /* */ for longer.

In this example, comments are used to explain the purpose of each line of code. The single-line comments explain the purpose of each variable, and the multi-line comment explains the purpose of the whole program.