Physics · Introductory

The C++ Programming Language

Written by SignalsLab

Early edition: these lessons are being reviewed.

Content

Summary

C++ programs begin execution at the main() function, which returns an integer to the operating system to signal success or failure. Source code is translated into binary via a compiler, utilizing headers like iostream for input/output operations and strict syntax rules such as semicolons and case sensitivity.

Intuition

Think of a C++ program as a recipe that the computer must follow precisely. The main() function is the starting point, like the first step in a cooking guide. Before the computer can cook (execute), it must translate your written instructions (source code) into a language the machine understands (binary) using a compiler. Just as you need specific ingredients (headers like iostream) to perform actions like printing to the screen (cout), you must include the right files. The compiler is strict: it cares about every semicolon, every capital letter, and ensures you don't mix up integer math with decimal math, which can lead to surprising results if not handled correctly.

Derivation

  1. The program execution initiates by invoking the main() function.
  2. The int keyword indicates the function returns an integer value to the OS.
  3. The function body is enclosed in curly brackets { }.
  4. Statements are strings of characters ending with a semicolon ;.
  5. Preprocessing handles directives like #include to insert header file contents.
  6. The compiler translates the source code into binary commands.
  7. Execution occurs after successful compilation and linking.

Worked example

Problem

Write a minimal C++ program structure that prints 'Hello' to the standard output.

  1. Include the iostream header to access standard input/output operations.
  2. Use using namespace std to allow direct access to cout.
  3. Define the main() function with int return type.
  4. Use cout << "Hello" << endl; to print the string.
  5. Return 0 to indicate successful execution.

Answer #include <iostream>
using namespace std;
int main() {
cout << "Hello" << endl;
return 0;
}

Common mistakes

  • Forgetting the semicolon ; at the end of a statement.
  • Using Main() or MAIN() instead of main() due to case sensitivity.
  • Assuming integer division behaves like floating-point division (e.g., 2/4 yields 0).
  • Forgetting to include the necessary header file (e.g., iostream) before using cout.

Animation

Animation coming soon

The animated version of this lesson is being prepared for the web. In the Signals AI app, lessons are drawn step by step.

Code

This is a text lesson: it has no animation code yet.

ccBYNC© 2026 SignalsLab. Lesson text shared as is under CC BY-NC 4.0.