DSA SheetLesson · no judge

PROGRAMMING FUNDAMENTALSC++ FUNDAMENTALS

Comments disappear before C++ interprets your source

Reading · 5 minQuiz · 5 questions2 code drills · run onlyGenerated by gpt-5.6-luna · Aug 22 · reviewed Aug 23

Comments remain for readers but disappear before C++ interprets the source

CPPThe complete source file, including one line comment and one block comment.
class Notes {
    int shown = 7; // 7 is active source.
    /*
    int hidden = 9;
    */
};

The compiler does not treat every character in this file as an instruction. The characters // and the text after them on the same line form a comment. The characters /* through the next */ form another comment. Those comment characters and the text between them are removed before C++ interprets the remaining source.

The line containing the literal 7 remains active source except for its comment. The declaration containing the literal 9 is entirely inside the block comment, so it is ignored. Its text can still help you understand the file, but it does not affect what the compiler interprets.

CHECKPOINT 1Not answered

After comments are removed from the source file, which literal remains in active source?

Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.

A // comment stops at the next line break

On the line containing 7, the // marker starts a line comment. The comment includes the space after the marker, the words 7 is active source., and every character after those words on that line. The next line break ends that comment. A line break is therefore part of the boundary: it does not belong to the comment text.

TEXTThe line comment starts at // and reaches the end of its line.
int shown = 7; // 7 is active source.
                 <------------------ comment ends at the line break

The /* marker on the following line begins independently. The earlier // comment cannot continue onto that line, because its line has already ended. This gives the source file two separate comment spans: one ending at a line break and one ending at a closing marker.

A /* comment continues until the first */ marker

The block comment starts at /* and continues across the line break, the line containing the declaration with 9, and the next line until it reaches */. Line breaks do not end a block comment. Every character in that span is comment text, including characters that look like C++ source.

TEXTThe block comment crosses three lines and ends at the first closing marker.
/*
int hidden = 9;
*/
start                 end

A // marker inside this block span would also be ordinary comment text. The block comment has already started, so that line marker cannot change how the block ends. The first */ marker ends the block. C++ does not support nested block comments, so a second /* inside the span does not create a separate inner block that must be closed first.

CHECKPOINT 2Not answered

If the closing marker is removed from the block comment, type the exact characters that end it.

class Notes {
    int shown = 7; // 7 is active source.
    /*
    int hidden = 9;
};

Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.

Comment delimiters can hide source or accidentally hide too much

The block around the declaration containing 9 can have either of two purposes. It can intentionally disable that source while keeping it in the file, or it can explain something to a reader. In both cases, the compiler sees only a comment span and does not interpret the characters inside it. The meaning for readers does not change the meaning for C++.

A comment delimiter is only useful when its pair is correct. If you delete the closing */, the block comment continues past the declaration containing 9 and consumes the rest of the file as comment text. Required source after that point can disappear from the compiler's input, which can cause compilation to fail.

Previous · LiteralsNext part · Quiz