How to Effectively Use and Fix Comments in C++

2025-09-01

Comments are notes within your code that the compiler ignores. They're essential for explaining what your code does, making it easier for you and others to understand and maintain. The two main types of comments in C++ are

Single-line comments
These start with a double slash // and comment out everything until the end of the line.

Multi-line comments
These start with /* and end with */. They're useful for commenting on multiple lines or for temporarily disabling a block of code.

This is a very common mistake. If you open a multi-line comment with /* but forget to close it with */, the compiler will treat all subsequent code as part of the comment. This can lead to a long list of compiler errors because the compiler doesn't "see" the rest of your code.

Bad Example

#include <iostream>

int main() {
    /* This is a multi-line comment.
    std::cout << "Hello, World!" << std::endl;
    // Oops, I forgot to close the comment!
    return 0;
}

How to Fix It

Simply add the closing */ where the comment should end.

Correct Example

#include <iostream>

int main() {
    /* This is a multi-line comment. */
    std::cout << "Hello, World!" << std::endl;
    return 0;
}

C++ does not allow you to nest multi-line comments. If you try to place a /* ... */ comment inside another one, the compiler will get confused. The outer comment will end at the first */ it finds, leaving the code that follows still commented out and the inner */ as an error.

Bad Example

/*
    This is the outer comment.
    /* This is an inner, nested comment. */
    This is more of the outer comment.
*/

How to Fix It

Use single-line comments for the inner part, or restructure your comments to avoid nesting.

Correct Example

/*
    This is the outer comment.
    // This is a nested comment using a single-line style.
    This is more of the outer comment.
*/

While // and /* ... */ are the standard, there are other ways to use them more effectively to improve code clarity and maintenance.

For larger projects, you might want to use a documentation tool like Doxygen. Doxygen uses a specific style of comments to automatically generate documentation for your code. This is great for creating professional-looking guides for your software. The most common Doxygen style starts with / for functions or files.

Example

/**
 * @brief This function adds two integers.
 * @param a The first integer.
 * @param b The second integer.
 * @return The sum of the two integers.
 */
int add(int a, int b) {
    return a + b;
}

Sometimes you need to temporarily disable a large section of code for debugging. You can use multi-line comments for this, but a more reliable trick is to use preprocessor directives. This is particularly useful because you can nest these blocks, unlike /* ... */ comments.

Example

#include <iostream>

int main() {
    std::cout << "Starting the program." << std::endl;

#if 0
    // This entire block of code is commented out!
    std::cout << "This line will not be printed." << std::endl;
    int x = 10;
    int y = 20;
    /* You can even have multi-line comments inside this block without issues. */
#endif

    std::cout << "Program finished." << std::endl;
    return 0;
}

The #if 0 and #endif block tells the preprocessor to simply ignore everything in between. It’s a very handy and robust way to "comment out" large chunks of code, especially for debugging.

Keep it simple
Comments should explain the "why," not just the "what." For example, instead of // Add a and b, a better comment would be // Add user input 'a' to 'b' for total score.

Don't over-comment
Clean, self-documenting code is often better than a lot of redundant comments. Choose clear variable names and function names.

Use them for debugging
Temporarily add comments to see how your program behaves without a specific line or block of code.


cpp



C++ Unordered Map Clear: Preventing Memory Leaks with Smart Pointers

The function std::unordered_map::clear() is a member function of the std::unordered_map class in C++. Its purpose is quite straightforward it removes all elements from the unordered map


Mastering std::multiset::crbegin: Common Pitfalls and Safe Usage

First, let's understand what std::multiset::crbegin is for. A multiset is a type of container in C++ that stores elements in sorted order


Mastering C++20 Ranges: A Deep Dive into take_view

While std::ranges::take_view is great, there are a few common pitfalls you might run into.The Problem What happens if you try to take 10 elements from a vector that only has 5? The take_view will happily give you a view of the first 5 elements and stop there



Mastering C++ do_thousands_sep: A Friendly Guide to Number Formatting

In C++, the do_thousands_sep function is a virtual function in the std::numpunct class. Its job is to tell a locale how to separate groups of digits in a number


C++ std::regex_traits::transform_primary: The Pitfalls of Locale-Aware Matching

The function std::regex_traits::transform_primary is a member of the traits class used by std::basic_regex. Its purpose is to get the primary sort key for a sequence of characters


A Friendly Guide to C++ Random Number Operators

The std::student_t_distribution is a C++ class in the <random> header that generates random numbers following a Student's t-distribution


Optimizing C++ std::multimap Insertion with emplace_hint

Hello there! Let's talk about std::multimap::emplace_hint in C++. It's a great tool, but like any powerful feature, it can sometimes be a bit tricky


Mastering std::chrono::gps_clock::now(): C++20 Time, Pitfalls, and Alternatives

Here is a friendly explanation of common issues and some helpful alternatives, along with sample code.First off, what is gps_clock::now()?


The C++ std::is_rvalue_reference Explained with Examples

Let's dive into some of the common issues and how to deal with them.First, a quick refresher. std::is_rvalue_reference is a type trait in the C++ standard library


Handling Regex Errors in C++: A Guide to std::regex_constants::error_type

Hey there! Let's talk about std::regex_constants::error_type in C++. This is an important part of handling errors when you're working with regular expressions