Lesson 1 - Your First C++ Program
Prerequisites
Section titled “Prerequisites”- None. This is the first lesson in the beginner path.
- A working C++ setup that can build and run a single source file.
What You Will Learn
Section titled “What You Will Learn”- The stages that turn C++ source text into a running program.
- What a complete, minimal C++ program looks like.
- How to make a program print one exact line of text.
From Source To A Running Program
Section titled “From Source To A Running Program”C++ is a compiled language. The text you write is not what the machine runs. Your text passes through a fixed sequence of stages first.
source | | compile vobject code | | link vexecutable | | run voutputRead the stages like this:
sourceis the C++ text you write and save in a filecompiletranslates one source file into machine instructionsobject codeis that translated result, which is not runnable on its ownlinkjoins your object code with the standard library code it depends onexecutableis the finished program file the system can startrunstarts the executable, which then produces output
Two ideas from this sequence matter for the rest of the path:
- A mistake in your text is usually reported at the compile stage, before anything runs
- Nothing you write produces output until the run stage is reached
Worked Example
Section titled “Worked Example”This is a complete program. Nothing is missing from it.
#include <iostream>
int main() { std::cout << "Hello, CP4CPP!\n"; return 0;}Output:
Hello, CP4CPP!Each part has a job:
#include <iostream>brings in the standard input and output declarations. Without it,std::coutis not declared.int main()declares the function where the program starts. Every program in this path has exactly onemain.{and}mark the start and end of the body ofmain.std::coutis the standard output stream. Thestd::prefix says the name comes from the standard library.<<sends the value on its right into the stream on its left."Hello, CP4CPP!\n"is the text to print. The\nis one newline character, so the output ends with a line break.return 0;endsmainand reports success to the system.
Exercise
Section titled “Exercise”Write a complete program that prints exactly one line:
I compiled my first C++ program.Start from the worked example and change only the text between the quotation marks. Keep #include <iostream>, int main(), the \n, and return 0; exactly as they are.
Exercise Check
Section titled “Exercise Check”Expected output:I compiled my first C++ program.This program reads no input, so there is nothing to type when it runs.
Check these before moving on:
- The line matches the expected output exactly, including the final period
- There is no extra text before or after the line
- The program reached the run stage, which means compiling and linking both succeeded
Next Steps
Section titled “Next Steps”You can now produce fixed output. The next lesson keeps this program shape and adds input, named variables, and arithmetic, so the output depends on what the program is given.