Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C++

Comments and documentation

C++ provides // comments extending to the end of a line and /* ... */ block comments. They let you explain code and write documentation without executing their contents. Block comments cannot be nested.

Version: C++23; repository compiler flag, production runtime not attested

Syntax

// comment
/* comment */

A // comment ends immediately before the next newline character. A /* ... */ comment can span multiple lines and ends at the first */. Block comments do not nest: another /* does not start an inner comment.

During translation, each comment is replaced by one space, while newline characters are retained. Thus int/**/value separates int and value, whereas va/**/lue does not form the identifier value. Line splicing occurs before comments are recognized: a \ at the end of a line containing a // comment can make the comment consume the next physical line too. Avoid this spelling.

Documentation forms such as /// and /** ... */ are ordinary comments to the language; interpreting them as documentation depends on separate tools. Prefer explaining purpose, assumptions, and constraints rather than restating code instructions. In the example, the /// comment describes add, and the block comment separates tokens in an expression.

This entry targets C++23. The supplied source is an evolving working draft, not a frozen C++23 edition. A repository compiler-mode setting does not attest to the production runtime environment.

Examples

#include <iostream>

/// Return the sum of two integers.
/// Precondition: The mathematical sum of a and b is representable as int.
int add(int a, int b) {
    return a /* first operand */ + b;
}

int main() {
    // Print the result once.
    std::cout << add(2, 3) << '\n';
}

Result

5

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources