Turning Learners Into Developers
Codekilla
CODEKILLA
back to course
Lesson 05 / 3913%· free preview
Getting Started5/5

C Comments

Master the use of comments in C to write clear, maintainable, and readable code.

Definition

Comments in C are non-executable lines that explain your code. They help programmers and readers understand logic, debug issues, and document code, but are ignored by the compiler and do not affect program output or performance.

Why Use Comments
  • Improve code readability.
  • Help others (and yourself) understand logic later.
  • Make debugging easier.
  • Temporarily disable code during testing.
  • Useful for documentation in large projects.
Types of Comments in C

There are two main types:

  • Single-line Comments (//)
  • Multi-line Comments (/* */)
1. Single-line Comments (//)

Single-line comments start with // and continue until the end of the line.

Syntax:

c
// This is a comment

Example 1: Comment Before Code

c
#include <stdio.h>

int main() {
    // This line prints Hello World on screen
    printf("Hello World!");
    return 0;
}
Output
text
Hello World!

Example 2: Comment at End of Line

c
#include <stdio.h>

int main() {
    printf("Hello World!"); // Printing message
    return 0;
}

Use this when you want to explain a specific line.

Example 3: Disable Code Using Comments

c
#include <stdio.h>

int main() {
    printf("This will run\n");
    // printf("This will NOT run\n");
    return 0;
}

Here, the second printf is disabled using a comment.

2. Multi-line Comments (/* */)

Multi-line comments are used for longer explanations or to comment out blocks of code over multiple lines.

Syntax:

c
/* This is a multi-line comment */

Example 1: Block Description

c
#include <stdio.h>

int main() {
    /* 
       This program prints Hello World
       It is a basic C program example
    */
    printf("Hello World!");
    return 0;
}

Example 2: Comment Multiple Lines of Code

c
#include <stdio.h>

int main() {
    /*
    printf("Line 1\n");
    printf("Line 2\n");
    */
    printf("Only this line will execute\n");
    return 0;
}

Both printf statements inside the comment are ignored.

Single-line vs Multi-line Comments
FeatureSingle-line (//)Multi-line (/* */)
UsageShort notesLong explanations
Lines SupportedOne lineMultiple lines
ReadabilityQuick commentsDetailed description
Common UseInline commentsBlock comments
Best Practices for Using Comments
  • Keep comments short and meaningful.
  • Avoid obvious comments, such as:
c
int a = 10; // Assign 10 to a (not useful)
  • Write helpful comments:
c
int a = 10; // Initial value for score
  • Remove unnecessary comments before final code.
  • Update comments when code changes.
Historical Note

Before the C99 standard, C supported only multi-line comments (/* */). Single-line comments (//) were introduced later with C99.

Key Takeaways
  1. Comments are used to explain code for human readers and are ignored by the compiler.
  2. C supports single-line (//) and multi-line (/* */) comments.
  3. Use comments to improve code readability and maintainability.
  4. Avoid unnecessary or obvious comments; favor meaningful explanations.
  5. Always update comments to reflect code changes accurately.
Interview Questions

Practice Questions
  1. Add a single-line comment to explain what printf("Hello"); does in a simple C program.
  2. Write a C block that uses multi-line comments to describe its purpose.
  3. Comment out one line in a three-line function and observe the output.
  4. Convert an obvious comment into a more meaningful one for clarity.
  5. Remove unnecessary comments from a sample C program segment.
Pro Tips
  1. Keep comments relevant by updating them when modifying code.
  2. Avoid stating the obvious and focus on explaining complex logic.
  3. Use single-line comments for inline explanations and multi-line for block documentation.
  4. Remove redundant comments before releasing your code.
  5. Use comments to temporarily disable code during testing, but remember to remove them later.
AI-powered recap

Quick recap quiz?

We'll generate 5 MCQs from this lesson and check your understanding instantly. Takes ~30 seconds.

# program

Program

C
#include <stdio.h>

int main(void) {
    // Print a greeting message
    printf("Hello, World!\n");
    /*
       The next line is commented out and won't execute
       printf("This line is disabled by a multi-line comment\n");
    */
    printf("Comments make code clear.\n");
    return 0;
}
Ready to move on?
// example library
Practice C Comments with runnable code
Browse 360 runnable examples · across 36 chapters · short, copy-paste-friendly · grouped by topic
Open chapter
// suggested companion course
Data Structures with C
Comfortable with C? Level up — arrays · linked lists · stacks · queues · trees · graphs · sorting algorithms.
Open course
// sharpen your skills
Put this into practice right now
LeetCode-style problems, graded difficulty, hints and expected outputs — learning beats passively reading every time.
Start Practicing
// feedback.matters()
Did this lesson help you?