Skip to content

Comments in C: The Notes in Your Code

In C programming, comments are like notes you add to your code to explain, clarify, and document what your code does. They are essential for making your code more readable and maintainable, especially when working with others or revisiting your code after some time. Let’s dive into the world of comments and see how they can enhance your coding experience.

What Are Comments?

Comments are pieces of text embedded in your code that are ignored by the compiler. They are meant for human readers, providing explanations and insights into the code’s purpose and functionality.

Types of Comments

C supports two types of comments:

1. Single-Line Comments: The Quick Notes

Single-line comments are used for brief explanations or notes on a single line. They start with // and continue to the end of the line.

Example

c
#include <stdio.h>

int main() {
    int number = 5; // This is a single-line comment
    printf("Number: %d\n", number); // Printing the number

    return 0;
}

2. Multi-Line Comments: The Detailed Explanations

Multi-line comments are used for longer explanations or to comment out blocks of code. They start with /* and end with */. You can span them across multiple lines.

Example

c
#include <stdio.h>

int main() {
    /*
     * This is a multi-line comment.
     * It can span multiple lines and is useful for
     * longer explanations or commenting out larger blocks of code.
     */
    int number = 10;
    printf("Number: %d\n", number);

    return 0;
}

Best Practices for Using Comments

To make the most of comments, follow these best practices:

Be Clear and Concise

Write comments that clearly explain what the code does. Avoid vague or overly complex explanations.

Avoid Redundancy

Don’t state the obvious. For example, comments like int a = 10; // Set a to 10 are redundant if the code is self-explanatory.

Update Comments

Keep comments up-to-date with code changes. Outdated comments can be misleading and confusing.

Use Comments for Documentation

Use comments to describe the purpose of functions, parameters, and return values. This helps others (and future you) understand how to use your code.

Example

c
#include <stdio.h>

/*
 * Function: addNumbers
 * ----------------------
 * Adds two integers and returns the result.
 *
 * a: The first integer.
 * b: The second integer.
 *
 * returns: The sum of a and b.
 */
int addNumbers(int a, int b) {
    return a + b; // Add the two numbers
}

int main() {
    int result = addNumbers(5, 7);
    printf("Result: %d\n", result);

    return 0;
}

Practice Exercise

Write a program that includes both single-line and multi-line comments. Ensure that you provide meaningful explanations for the purpose of your code and its various sections. Experiment with commenting out different parts of your code to understand how comments can be used for both documentation and debugging.


Comments are the notes and explanations that make your code easier to understand and maintain. They are an essential part of writing clean, effective code. Next, let’s explore the Variables chapter to understand how to manage and utilize data within your programs effectively.