Building Your First C++ Program
A C++ program is a set of instructions that the computer must follow exactly.
Why Programs Need Notes for Readers
A C++ program is a set of instructions that the computer must follow exactly. A program is not only read by machines, though. Programmers need to read that code too, sometimes months after they wrote it, or when a teammate has to change it. A way to leave explanations inside the source file, aimed only at people, solves this problem. In C++, these explanations are called comments.
What do you think happens?
If you delete a comment line from a program, does the program's behavior change?
Reveal answer
No, the program does the same thing but reads differently to a person.
Removing a comment changes how the program reads to a person, but it does not change what the program does. That difference is the whole purpose of comments: they make code easier for humans to understand while the compiler skips them.
What the Compiler Actually Reads
Comments are written for the human reader, and the compiler ignores them. Before the compiler reads the code, comment lines are removed. This means the compiler never sees the comment text at all. What reaches the compiler is the code that remains after those comment parts have been taken out.
Trace the flow in the diagram. The source file holds both code and comments. The comment step removes the comments, and only the remaining text moves on. The compiler therefore works from a version of the file that has no comment text in it. The braces and statements of the program are not comments, so they remain in that input and the compiler does read them as part of the program's structure.
Curly Braces as Grouping
Before looking at comments in detail, it helps to understand the curly braces that often appear beside them. In C++, curly braces, written as the pair of symbols { and }, express grouping. They collect a set of statements together and show where that group begins and ends.
In a function, the braces wrap the function's body. Everything between the opening brace and the closing brace belongs to that function. The braces are grouping symbols rather than comments, so the compiler reads them as part of the program's structure.
- The opening brace starts the function body
- The comment is inside the braces, so it belongs to
show_message - The closing brace ends the function body
Read this program first as a programmer would. The function is named show_message, and its body is the region between the two braces. The comment sits inside that region, explaining a line for whoever reads the file. A reader can see at a glance where the function starts and stops, while the compiler uses the same braces to recognize the function's extent.
Double-Slash Comments to the End of a Line
A comment in C++ begins with a double slash, written //. The comment then extends to the end of the line. Everything from the double slash to the end of that line is the comment, and the next line of code starts fresh. Because the comment stops at the line break, you can place one comment on its own line or after code on the same line.
- A full-line comment begins with // and runs to the end of its line
start_engine(); comes before the double slash, so it is not part of the comment- Everything after the double slash on that line is hidden from the compiler
void show_message() {
// This is a note for the reader.
}The source file contains a comment line inside the function body.
void show_message() {
}The comment text is removed before the compiler reads the code, and the function body is unchanged in meaning.
What do you think happens?
In start_engine(); // stop_engine(); does the compiler treat stop_engine() as part of the program?
Reveal answer
No, only start_engine() is part of the program.
A double slash hides everything from it to the end of the line. Anything after // on that line is comment text, so stop_engine() is ignored, and the next line of code starts fresh.
Common Mistakes with Comments
Common mistake Assuming the compiler reads comment text
What it looks likeWriting a note such as // TODO fix this and expecting it to affect the program's output
Why it failsComment lines are removed before the compiler reads the code, so the compiler never sees their text.
Do this insteadUse comments to explain code to people, and write the instructions the program needs as actual code.
Common mistake Writing code after a double slash and expecting it to run
What it looks likestart_engine(); // stop_engine(); with the expectation that both calls execute
Why it failsThe double slash turns everything to the end of the line into a comment.
Do this insteadPlace any code you need before the double slash, or move it onto its own line.
Common mistake Confusing braces with comments
Why it failsCurly braces are grouping symbols, and the compiler reads them as part of the program's structure.
Do this insteadTreat the braces as real code that marks where a function body starts and ends.
Where Getting Started Fits
Any course that teaches a programming language has to answer a practical question before it teaches the language itself: how does a learner get started at all? The Getting Started chapter exists to answer that question. Its job is to establish a working starting point so that the material that follows has a place to land.
Think of Getting Started as the first step in a sequence rather than a body of technical content. The chapter does not ask you to master a large amount of C++. It sets the stage for the modules that do. This article is course orientation: it describes how the course is organized and what kind of first step Getting Started represents. It does not walk through installing tools or running a program. Those details belong to the course's environment instructions.
Course Order After Getting Started
The course is organized as a sequence of chapters, each made up of one or more modules. Getting Started is the first chapter. The order matters because later topics build on earlier ones. The next chapter, Building Your First C++ Program, is where the course begins teaching program construction in earnest. Its modules cover C++ Program Fundamentals, Output and Input/Output Operators, and Functions in C++.
After Building Your First C++ Program, the course moves through a run of foundational topics. These include function basics and function overloading, variables and types with arithmetic and type conversions, variable scope and lifetime, constants, and pointers, arrays, and references. Later chapters address conditional statements and loops, best practices for the basics, and then the structure of C++ type systems.
The modules that build most directly on the first steps are the ones that use the same ideas you meet here: functions, which the Functions in C++ module introduces and which the later function basics and overloading topics extend, and the input and output material in Output and Input/Output Operators, which every later example relies on to show results. Because these earlier topics come first, skipping ahead usually makes the later material harder to follow.
Practice Reading a Program
Read the following function. Decide which lines the compiler reads, and which lines are ignored. Then say whether the braces are part of the program's structure. Program: void greet() { // print a hello message open_door(); // close_door(); }
Hints
- Anything from a double slash to the end of its line is comment text
- Braces are grouping symbols, not comments
- Only the code before the double slash on each line remains for the compiler
Key Takeaways
- Comments are written for the human reader, and the compiler ignores them; comment lines are removed before the compiler reads the code.
- Curly braces express grouping: an opening brace and a closing brace mark the start and end of a function body, and the compiler reads them as structure.
- A double-slash comment begins with // and extends to the end of the line, so anything after // on that line is hidden.
- Getting Started is the first step in the course and sets a starting point; Building Your First C++ Program comes next, and later topics build on these early ones.