Comments in C

Learn via video course
FREE
View all courses
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
Topics Covered

Comments in C are program text used to explain the program logic. They are ignored by the compiler. Comments help to make our code more readable and maintainable. The compiler and interpreter ignore comments, so they do not affect the program's behaviour or performance.

Syntax:

When do We Use Comments in C?

We use comments in many cases, a few of the cases are:

  1. Code Explanation:

    • Example: int x = 10; // Initialize variable x with value 10.
    • Explanation: Clarifies the purpose or initialization of a variable.
  2. Clarification for Others:

    • Example: for (int i = 0; i < 5; i++) { /* Loop to iterate through elements */ }
    • Explanation: Provides context or purpose for code blocks, making it easier for others to understand.
  3. Disabling Code Temporarily:

    • Example: /* Commented-out code for debugging purposes */
    • Explanation: Temporarily disables a section of code without removing it.
  4. Documentation of Functions:

    • Example: /* Function to calculate area */ int calculateArea(int length, int width) { return length * width; }
    • Explanation: Documents functions, including parameters and purpose.
  5. TODO Comments for Future Work:

    • Example: // TODO: Implement error handling for input validation.
    • Explanation: Marks tasks or improvements to be addressed in the future.
  6. Explanations for Non-Obvious Code:

    • Example: for (int i = 1; i <= 10; i += 2) { /* Increment by 2 to skip even numbers */ }
    • Explanation: Adds comments to clarify non-obvious logic or behaviors in the code.

Types of C Comments

  • Single-Line Comments
  • Multi-Line comments or Paired Comments

Types of comments in c

Single Line Comment in C

A Single Line Comment starts with a double forward-slash (//) and ends with a new line. So, everything to the right side of these forward slashes on the current line is ignored by the compiler. A comment of this type can contain any text, it can even contain another double slash as well. They best suit when we have to give short detail about the code.

Example of Single Line Comment: This C program illustrates the Single Line Comment

Output:

In the above example, we can see that the commented line is ignored by the compiler in the program.

Multi-Line Comment or Paired Comment in C

Multi-Line comment uses two delimiters /* and */ which is a slash asterisk /* and asterisk slash */. Multi-Line comments start with a slash asterisk /* and end with the asterisk slash next */. These comments can include anything that is not a */ , including newlines. The compiler treats everything as comments, whichever comes between the /* and */. This type of comment is used when we have to give a detailed explanation of the code and require more than one line.

Example of Multi-Line or Paired Comment:

This C program illustrates the Multi-Line Comment

Output:

In the above example, we can see that everything between the delimiters is ignored by the compiler in the program.

Note: A comment that begins with /* ends with the */ means Comment Pairs do not nest. As a result, one comment pair can not appear inside another.

What does the compiler do with the C comments ?

Lexical Analyzer is an in-build program in the compiler that scans the characters and transforms them into tokens and these in-build programs do not pass the commented text to the parser. That means comments are simply omitted at the time of compilation as they are just there for reading purposes and do not contribute to the program's functionality.

Hence we can understand that Comments are program text that is ignored by the compiler.

Advantages of commenting on a program

  • Using comments in a program, the program seems easy to read and maintain.
  • By using comments to explain the program's working and logic made the program universally accepted as everyone finds it easy to use and understand.
  • Using comments not only others but you can also understand your code after ages.

Conclusion

  • Comments in C are used to explain the code we are writing in our program.
  • Using Comments we can make our code more readable, and maintainable and error finding becomes easier.
  • C supports two types of commenting styles one is a single-line comment and the other is a multi-line comment.
  • The compiler just ignores the commented text at the time of compilation.
  • A good programmer always uses comments in their code.