C Notes
Learn about comments in C — single-line comments, multi-line comments, best practices, nested comments, and when to use comments effectively in your code.
Have you ever gone back to code you wrote a few months ago and struggled to understand what it does? That's exactly the problem comments solve. Comments are human-readable notes added to source code that are completely ignored by the compiler. They exist purely for the benefit of programmers — whether that's your future self, your teammates, or anyone who reads your code.
Writing good comments is a skill that separates amateur programmers from professionals. Let's explore how comments work in C, the different types, and when you should (and shouldn't) use them.
Types of Comments in C
C supports two styles of comments:
| Type | Syntax | Introduced In |
|---|---|---|
| Multi-line (block) comment | /* ... */ | Original C (K&R, C89) |
| Single-line comment | // ... | C99 (borrowed from C++) |
Single-Line Comments (//)
Single-line comments begin with // and continue until the end of that line. Everything after // on the same line is treated as a comment.
#include <stdio.h>
int main() {
int age = 25; // stores the user's age
float gpa = 3.8; // grade point average
// Print student information
printf("Age: %d\n", age);
printf("GPA: %.1f\n", gpa);
return 0; // indicate successful execution
}Age: 25 GPA: 3.8
When to Use Single-Line Comments
- Brief explanations beside a line of code
- Temporarily disabling a single line during debugging
- Short notes about logic or decisions
int maxRetries = 3; // server timeout policy requires max 3 retries
// TODO: Add input validation here
// FIXME: This calculation overflows for large inputsMulti-Line Comments (/* ... */)
Multi-line comments start with /* and end with */. Everything between these markers is ignored, regardless of how many lines it spans.
#include <stdio.h>
/*
* Program: Temperature Converter
* Author: John Smith
* Date: 2024-03-15
* Description: Converts temperature from Celsius to Fahrenheit
*/
int main() {
float celsius, fahrenheit;
/* Get temperature input from user */
printf("Enter temperature in Celsius: ");
scanf("%f", &celsius);
/*
* Conversion formula:
* F = (C × 9/5) + 32
*/
fahrenheit = (celsius * 9.0 / 5.0) + 32.0;
printf("%.2f°C = %.2f°F\n", celsius, fahrenheit);
return 0;
}Enter temperature in Celsius: 100 100.00°C = 212.00°F
When to Use Multi-Line Comments
- File headers (author, date, description)
- Function documentation explaining parameters and return values
- Disabling multiple lines of code during debugging
- Complex algorithm explanations
Commenting Out Code (for Debugging)
One common use of comments is temporarily disabling code while debugging. Both comment styles work for this purpose.
#include <stdio.h>
int main() {
int x = 10, y = 20;
// Temporarily disabled for testing
// x = getUserInput();
// y = getUserInput();
/*
printf("Debug info:\n");
printf("x address: %p\n", &x);
printf("y address: %p\n", &y);
*/
printf("Sum: %d\n", x + y);
return 0;
}Sum: 30
Pro tip: Version control (like Git) is a better alternative to commenting out code. Commented-out code clutters the file and often stays forever.
Nested Comments in C
A very important thing to know: C does not support nested multi-line comments. The first */ encountered will end the comment, regardless of any opening /* inside.
/* This is a comment
/* This nested comment BREAKS things */
This line causes a compilation error! */Here's what the compiler sees:
Workaround for Nested Comments
If you need to comment out a block that already contains /* */ comments, use // for the outer layer or #if 0:
// Method 1: Use single-line comments
// /* Original comment */
// int x = 10;
// int y = 20;
// Method 2: Use preprocessor directive (preferred)
#if 0
/* Original comment */
int x = 10;
int y = 20;
#endifThe #if 0 ... #endif approach is widely used because:
- It handles nested comments correctly
- It supports any content between the directives
- It clearly signals "this code is disabled"
Comment Styles and Formatting
File Header Comment
/************************************************************
* File: calculator.c
* Author: Jane Doe
* Date: 2024-06-10
* Description: Basic arithmetic calculator with error handling
* Version: 2.1
************************************************************/Function Documentation Comment
/**
* Calculates the factorial of a non-negative integer.
*
* @param n The non-negative integer (must be <= 20 to avoid overflow)
* @return The factorial of n, or -1 if n is negative
*/
long factorial(int n) {
if (n < 0) return -1;
if (n == 0) return 1;
return n * factorial(n - 1);
}Inline Comments
Section Separators
/* ===================== */
/* HELPER FUNCTIONS */
/* ===================== */
void helperOne() { /* ... */ }
void helperTwo() { /* ... */ }
/* ===================== */
/* MAIN LOGIC */
/* ===================== */
int main() { /* ... */ }Best Practices for Writing Comments
DO ✓
- Explain WHY, not WHAT — the code shows what; comments should explain reasoning.
// ✓ GOOD: explains why
int timeout = 30; // 30 seconds per network team's SLA requirement
// ✗ BAD: just repeats the code
int timeout = 30; // set timeout to 30- Keep comments updated — stale comments that don't match the code are worse than no comments.
- Comment complex logic — algorithms, formulas, and non-obvious decisions deserve explanation.
// ✓ GOOD: explains non-obvious formula
// Using Bresenham's algorithm for efficient integer-only line drawing
int d = 2 * dy - dx;- Use TODO/FIXME markers for known issues or planned improvements.
// TODO: Replace with hash table for O(1) lookup
// FIXME: This fails when input array is empty
// HACK: Temporary workaround until API v2 is released- Document function interfaces — parameters, return values, side effects.
DON'T ✗
- Don't state the obvious — trust readers to understand basic C.
- Don't comment every single line — over-commenting clutters code.
- Don't leave commented-out code permanently — use version control instead.
- Don't write novels — be concise and clear.
Practical Example: Well-Commented Program
First 10 prime numbers: 2 3 5 7 11 13 17 19 23 29
How the Compiler Handles Comments
During the preprocessing phase (before actual compilation), all comments are replaced with a single space. This means comments have zero impact on:
- Program size (binary file)
- Execution speed
- Memory usage
This also means you cannot use comments to create tokens:
// This does NOT work:
int my/**/variable; // compiler sees: int my variable; → ERRORComments and Strings — An Important Distinction
Comment markers inside string literals are NOT treated as comments:
#include <stdio.h>
int main() {
// The // and /* inside strings are just characters
printf("Visit http://example.com\n");
printf("/* This is NOT a comment */\n");
printf("Use // for single-line comments\n");
return 0;
}Visit http://example.com /* This is NOT a comment */ Use // for single-line comments
The compiler correctly identifies that these are inside string literals and treats them as regular characters.
Interview Questions on Comments in C
Q1: What are the two types of comments in C? Which one was added later?
C has block comments (/* ... */) and single-line comments (// ...). Block comments were part of C from the beginning (K&R C). Single-line comments were officially added in the C99 standard, though many compilers supported them earlier as a C++ extension.
Q2: Can comments be nested in C? What happens if you try?
Multi-line comments (/* */) cannot be nested in C. The first */ encountered ends the comment, regardless of any opening /* inside it. This causes a compilation error if there's code after the premature ending. The workaround is to use #if 0 ... #endif to disable blocks containing comments.
Q3: Do comments affect program performance?
No. Comments are completely removed during the preprocessing phase (replaced by a single space). They don't increase binary size, slow down execution, or affect memory usage in any way.
Q4: What is the difference between #if 0 and commenting out code?
#if 0 ... #endif is processed by the preprocessor and correctly handles any content between the directives, including nested /* */ comments. Regular /* */ comments cannot be nested, so they may fail if the disabled block already contains block comments. #if 0 is the preferred method for disabling large code sections.
Q5: Is // legal in C89/C90?
No, single-line comments (//) are not part of the C89/C90 standard. They were officially introduced in C99. However, most modern compilers (like GCC and Clang) support them even in C89 mode as an extension. If strict ANSI compliance is required, use only /* */ comments.
Summary
- Comments are non-executable notes for human readers — the compiler ignores them completely.
- C supports single-line (
//) and multi-line (/* */) comments. - Single-line comments extend from
//to the end of the line. - Multi-line comments span from
/*to the next*/and cannot be nested. - Use
#if 0...#endifto safely disable code blocks that contain comments. - Good comments explain *why* something is done, not *what* the code does.
- Comments don't affect program performance — they're removed during preprocessing.
- Follow consistent commenting styles for file headers, function docs, and inline notes.
- Avoid over-commenting, stale comments, and permanently commented-out code.
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Comments in C Programming.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this C Programming topic.
Search Terms
c-programming, c programming, programming, basics, comments, comments in c programming
Related C Programming Topics