C Comments


Writing code is only half the job in programming. The other half is making sure your code is understandable to others—and to yourself when you revisit it later. This is where comments come in. Comments are notes in your program that explain what the code does. They are ignored by the compiler, so they don’t affect how your program runs.

Understanding and using comments effectively is an essential part of writing clean, professional, and maintainable C programs.

What are Comments in C?

Comments in C are lines of text that are included in the source code for explanation purposes. They are meant for human readers, not the computer. The compiler skips them entirely.

In simple terms, comments are instructions or notes that help programmers understand the code’s purpose, logic, or flow without affecting the execution of the program.

Why Use Comments?

Comments are crucial in programming for several reasons:

  1. Code Readability:
    They make complex programs easier to read and understand.

  2. Documentation:
    Comments can describe what a particular function, block, or line of code does.

  3. Debugging Aid:
    You can comment out parts of your code temporarily while testing or debugging without deleting them.

  4. Collaboration:
    When multiple programmers work on the same project, comments explain the logic to others.

  5. Future Reference:
    If you revisit your code after months or years, comments remind you why certain decisions were made.

Types of Comments in C

C provides two types of comments:

1. Single-line Comments

Single-line comments start with //. Everything following // on the same line is treated as a comment.

Example:

#include <stdio.h>

int main() {
    int age = 20; // This variable stores the age of the user
    printf("Age: %d", age);
    return 0;
}

In this example, the compiler ignores the text This variable stores the age of the user.

2. Multi-line Comments

Multi-line comments start with /* and end with */. They can span multiple lines.

Example:

#include <stdio.h>

int main() {
    /* This program demonstrates multi-line comments
       It declares a variable, assigns a value, 
       and prints the value on the screen */
    int number = 10;
    printf("Number: %d", number);
    return 0;
}

This type of comment is useful for detailed explanations or documenting large sections of code.

Rules for Writing Comments

To use comments effectively, follow these rules:

  1. Do not nest multi-line comments:
    Nested /* ... */ inside another multi-line comment can cause errors.

  2. Keep comments clear and concise:
    Avoid writing long paragraphs unless necessary. Stick to the point.

  3. Explain ‘why’ not ‘what’ always:
    Instead of explaining obvious things (like int a = 5; // declare a variable), explain why you wrote the code.

  4. Avoid over-commenting:
    Too many comments can clutter the code and reduce readability.

Examples of Good Comments

#include <stdio.h>

int main() {
    // Store the user's age
    int age = 25;

    // Check if the user is eligible to vote
    if (age >= 18) {
        printf("Eligible to vote\n");
    } else {
        printf("Not eligible to vote\n");
    }

    return 0;
}

In this example, comments explain the purpose of the variables and the logic of the condition without stating the obvious.

Commenting Out Code

Comments are often used to temporarily disable parts of the code during testing or debugging. This is called commenting out code.

Example:

#include <stdio.h>

int main() {
    int x = 10;
    int y = 20;

    // int sum = x + y; // This line is ignored
    printf("Value of x: %d\n", x);
    printf("Value of y: %d\n", y);

    return 0;
}

Here, the sum calculation is temporarily disabled. You can easily re-enable it later by removing the //.

Best Practices for Comments

  1. Use meaningful comments:
    Instead of stating the obvious, explain the logic or intention behind the code.

  2. Keep comments up-to-date:
    Outdated comments can confuse others more than having no comments at all.

  3. Use multi-line comments for documentation:
    Use /* ... */ for functions, algorithms, or important sections of code.

  4. Use single-line comments for inline explanations:
    Use // for short explanations next to specific lines of code.

Example Program Using Comments

#include <stdio.h>

int main() {
    /* Program to calculate the area of a rectangle
       Steps:
       1. Declare length and width
       2. Calculate area = length * width
       3. Display the result
    */

    int length = 10; // Length of rectangle
    int width = 5;   // Width of rectangle

    int area = length * width; // Area calculation

    printf("Area of rectangle: %d\n", area); // Display result

    return 0;
}

Output:

Area of rectangle: 50

This program uses both single-line and multi-line comments to explain its logic and flow clearly.

Summary of the Tutorial

Comments are an essential part of writing professional C programs. They improve code readability, provide documentation, help in debugging, and facilitate collaboration. By using single-line and multi-line comments effectively, you can make your programs easier to understand for yourself and others.

Remember, good comments explain the intention behind the code, not just what the code does. Over time, developing the habit of writing meaningful comments will make your programming skills much stronger.


Practice Questions

  1. What is the purpose of comments in C programming?

  2. What are the two types of comments in C, and how are they written?

  3. Explain why C ignores comments during program execution.

  4. Identify the error in the following code:

    /* This is a multi-line comment
       /* Nested comment example */
       printf("Hello");
    
  5. Write a single-line comment explaining the purpose of a variable named totalMarks.

  6. How can comments be used for debugging or temporarily disabling code?

  7. Write a small C program with a multi-line comment explaining the steps of a calculation.

  8. Why is it better to explain “why” in comments rather than “what”?

  9. Give an example of a meaningful single-line comment and a meaningful multi-line comment.

  10. What are some best practices for writing comments in professional C programs?


Try a Short Quiz.

coding learning websites codepractice

No quizzes available.

Go Back Top