Step 01 · C++ essentials · B1
Your first C++ program
Turn a source file into a program, follow its values, and diagnose a small build error.
Before this page: no prior C++ or emulation knowledge required. View the learning route.
Before you start
No C++ or emulation knowledge is required. A program is a sequence of instructions. We write C++ source text; a compiler translates it into machine code, and a linker connects that code with the library functions it uses. An executable is the resulting file you can run. A debugger can pause it so you can inspect values.
Set up once
Use a C++20 compiler, CMake 3.20 or newer, and Ninja. On Windows, install Visual Studio C++ Build Tools with the Windows SDK and the C++ Clang tools, then open x64 Native Tools Command Prompt or Developer PowerShell. On Linux, use your distribution's C++ compiler, CMake and Ninja packages. On macOS, use the Xcode command-line tools plus CMake and Ninja. Run clang++ --version, cmake --version and ninja --version in that shell before continuing.
The following commands run from the learning repository root, the folder containing docs/ and scripts/. They configure and build only these small lessons. They do not build KytyPS5.
cmake -S docs/labs/beginner -B _Build/curriculum/beginner -G Ninja -DCMAKE_BUILD_TYPE=Debug -DCMAKE_CXX_COMPILER=clang++ cmake --build _Build/curriculum/beginner ctest --test-dir _Build/curriculum/beginner --output-on-failure
-S selects source, -B selects generated build files, and Debug asks for a debuggable build. The executable names match the source names. CTest runs them and checks exit status and exact output. A failed command is a reason to inspect the first diagnostic, not to proceed as if the build worked.
Setup troubleshooting
- Command not found: reopen the developer shell after installing the tool and check the version commands.
- Cannot find standard headers or libraries on Windows: verify the C++ workload and SDK are installed, and use the x64 developer environment.
- A different compiler is cached: use a fresh build folder such as
_Build/curriculum/beginner-clangand use that folder in later commands. - Executable not found: check that the build succeeded. These instructions use Ninja; an IDE generator can place executables in a configuration subfolder.
Read the language in a small program
A variable names a value stored in an object. int total = 0; creates an integer initialized to zero. const prevents assigning a new value through that name. A function groups operations: pixel_count takes a Frame argument and returns an integer. Braces delimit its body, semicolons finish statements, and return gives its result to the caller.
A struct groups named members: a frame has width and height. The dot accesses a member. std::array<Frame, 2> stores two frames; the angle brackets supply the element type and count to a library template. You can use this container before learning to write templates. std:: identifies names from the standard library; #include makes their declarations available.
The range for loop visits each frame. Here it makes a small copy called frame. std::cout writes output; << sends the next value to that stream, and '\n' ends the line. Execution enters our main function. Returning zero reports success to the shell; our explicit nonzero return reports a failed check.
Read a decision before running it
= assigns a value. == compares two values for equality, and != asks whether they differ. In if (total != 16), the parenthesized condition decides whether the following block runs. If the total is wrong, return 1; leaves main immediately. Otherwise execution continues to return 0;.
Predict: what exit status would this check produce for totals 16 and 18? Does testing total != 16 change total?
Check your prediction
16 reaches the final return and exits with 0; 18 takes the failed-check branch and exits with 1. The comparison changes no value.
Predict, then run
This is a standalone C++ example; it compiles without KytyPS5. Before running, calculate each product and the final total on paper.
#include <array>
#include <iostream>
struct Frame {
int width;
int height;
};
int pixel_count(Frame frame) {
return frame.width * frame.height;
}
int main() {
const std::array<Frame, 2> frames{{{4, 3}, {2, 2}}};
int total = 0;
for (Frame frame : frames) {
const int pixels = pixel_count(frame);
std::cout << "pixels=" << pixels << '\n';
total = total + pixels;
}
std::cout << "total=" << total << '\n';
if (total != 16) {
return 1;
}
return 0;
}
Use the B1 setup once. From this learning repository's root:
cmake --build _Build/curriculum/beginner --target first_program ctest --test-dir _Build/curriculum/beginner -R '^beginner_first_program$' --output-on-failure
To see the output directly in PowerShell, run .\_Build\curriculum\beginner\first_program.exe; in a POSIX shell, run ./_Build/curriculum/beginner/first_program. Tests compare the full output and require a successful exit.
Expected output, with exit status 0:
pixels=12 pixels=4 total=16
Change one thing and explain it
- Change the second frame to width 3, height 2. Rebuild and predict the output before running. The old check should fail.
- Confirm that the new total is correct, then update the
total != 16check and the expected-output filefirst_program.txttogether. Re-run the focused test. - Temporarily remove the semicolon after
int total = 0. Rebuild, read the first compiler diagnostic, and restore it. - Open the source and debug executable in your debugger, set a breakpoint inside
pixel_count, and inspectframe.widthandframe.heighton both calls. Step out and inspecttotalafter each addition. If debugger setup is unavailable, keep a hand trace and mark the debugger step pending.
Hint
The total is accumulated across both frames. A passing check requires both the program's internal condition and the expected-output file to describe the behavior you intended.
Worked answer and rubric
The new products are 12 and 6, so the total is 18. Update the check to total != 18 and the last two expected lines to pixels=6 and total=18. The missing semicolon is a compile error; a declared function with no linked definition instead produces a linker error. You are ready when you can explain each output line and locate the input values in a trace or debugger.
Where this appears in KytyPS5
Later, read src/main.cpp: its main starts the program and delegates to other functions. src/emulator.h groups run inputs in a struct. Those functions perform much more work, but functions, calls, members and conditions are the same building blocks. You do not need the emulator checkout to finish this lesson.
For more practice, the optional LearnCpp introduction to C++ begins a longer language course.
Before moving on
Ticks record your own assessment. Keep your prediction, actual output and explanation; a tick alone is not evidence of understanding.