C++ Comments
This page adds one note that the compiler skips. The first program left pine.cpp with no comment, and ./pine showed pine. That word is still the result you are protecting. You type one comment above the writing line, rebuild with the same command, and the screen stays pine. The note itself never appears. Getting Started keeps pine.cpp and the lowercase word pine until a later page says the example changed.
You are keeping the compiler you already installed. If g++ or clang++ cannot be found, that gap is the install page. The next page is output. It is the page that adds a second writing line. This file still has one std::cout, and this page stops before a second one.
A comment is text for a person reading the file. The compiler throws that text away before it builds the program. In the checked file the comment is a line that starts with two slashes. Everything after // on that same line is the note. The next line is a normal statement again, so the writing line still runs.
Type the file in this order. The include, the blank line, main, the comment, the writing line, and return 0 under the writing line. main is still a free function. There is still no class named Pine. Do not add a second file. The name on disk stays pine.cpp.
#include <iostream>
int main()
{
// the word stays pine
std::cout << "pine" << std::endl;
return 0;
}
The new line is // the word stays pine. The two slashes are the mark. The words after them are the note. They are not inside quotes, so they are not a string the program sends. The compiler skips from // through the end of that line, including the spaces. It does not skip the next line. std::cout << "pine" << std::endl; still sends the four letters. return 0; still prints nothing, and it still sits under the writing line.
Rebuild from the folder that holds pine.cpp. The command is the one the first program used: g++ -std=c++17 pine.cpp -o pine and then ./pine. If this machine's compiler is clang++ because g++ is the one that is missing, swap only the tool name: clang++ -std=c++17 pine.cpp -o pine. Either way the screen is one line:
pine
The note is absent. The slashes are absent. You do not see "the word stays pine" under the word or above it. Count the letters on the screen. They are still p, then i, then n, then e. Four letters, no space, no capital, no second line. The first program showed those same four letters, and the comment did not add a fifth. If that sentence is on the screen, it was typed inside the quotes of the writing line, not after //. Put the quotes back to pine only, leave the note on its own line after //, and rebuild.
The comment is useful because you can hide a line without deleting it. Put // at the start of the writing line so the line becomes // std::cout << "pine" << std::endl; and rebuild. The compiler skips the write. ./pine shows an empty screen. The word from the first program is gone. That empty screen is the proof that // eats the rest of its own line, including a real statement. Delete those two slashes, leave the writing line as a statement, rebuild, and ./pine shows pine again. Do not leave the writing line commented out. The checked file comments the note, not the write.
A block comment starts with / and ends with /. Text between those marks is also skipped, and it may cover more than one line. You can put / not a second word / on its own line above the write, rebuild, and the screen is still pine. Then delete that block. If you open / and forget /, the rest of the file becomes the comment. The writing line can vanish into the note, the build can fail, and the screen is not pine. Close the block or delete it. The checked file uses the single line // the word stays pine and no block comment.
A few marks look similar and are not this comment. The quotes around pine keep a string. They do not start a comment. std::endl ends the printed line. It is not a note. The semicolon ends a statement. It is not a note. #include is a request for a library, and the # there is not //. A comment does not replace any of those. The table maps the note, the mark, the screen, and the build. The screen row is still the one word. The note row is the line the compiler skips.
📊 What the comment does to the screen
| Piece | On this page |
|---|---|
| Note | // the word stays pine |
| Marker | // |
| Screen | pine |
| Build | g++ -std=c++17 pine.cpp -o pine |
Comments do not change the order of the statements that remain. The include is still first. main still starts the program. The writing line still runs because it does not begin with //. return 0 still finishes the run and still prints nothing. Moving return above the write still empties the screen, comment or not. Put return back under the write before you judge the page. The comment stays where it is, above the write, on its own line.
Do not add a second std::cout. A second writing line would put another word on the screen, and that is a different page. Do not read the keyboard. Do not rename the file. Do not wrap main in a class. Getting Started still compiles this pine.cpp, still expects the lowercase word, and now also expects the one note that does not print.
The next page is output. Its job is a second writing line, so the screen can hold more than the one word pine. This page stops before that line. You leave with pine.cpp, one comment the compiler skips, one writing line, and a run that shows pine. You do not leave with a second word.
Last pass: the mark is //, the note is the word stays pine, the writing line is unchanged, return 0 is under it, the build is g++ -std=c++17 pine.cpp -o pine, and ./pine shows pine. The output page is next, and this page does not start it.
🧠 Test Your Knowledge
Test Your Knowledge
Challenge yourself with this interactive quiz and see how well you understand the topic
📝 Instructions
- Read each question carefully
- Select the best answer for each question
- You can retake the quiz as many times as you want
- Your progress will be shown at the top