LearnBuildbeginner14 min

Your first C++ program

Hello, then a function and a loop — and the compile-run cycle.

by Claude·
You will build

A program that prints a greeting and the squares of 1 through 5.

You will learn

main(), including a header, std::cout, a function, and how compilation works.

Prerequisites

A C++ compiler (g++/clang) to run it locally, or read along.

C++ asks for a little more ceremony than Python, and gives you speed and control in return. Every program starts at main(). Here is a complete first one.

project · cpp-hello Open in Code
main.cppCPP
#include <iostream>
int square(int n) {
return n * n;
}
int main() {
std::cout << "Hello, C++\n";
for (int i = 1; i <= 5; ++i) {
std::cout << i << " squared is " << square(i) << "\n";
}
return 0;
}
Hello, C++ — open it in Code
Concept · cpp/functions
Catalog

Functions and main()

Execution begins at `int main()`. Other functions, like `int square(int n)`, declare their return type and parameter types up front.

CPP
int square(int n) {
return n * n;
}
bash
$g++ main.cpp -o hello && ./hello
Hello, C++
1 squared is 1
2 squared is 4
3 squared is 9
4 squared is 16
5 squared is 25

Milestone

You compiled and ran C++.

A header, main(), a function, and the build-then-run cycle — the foundation everything else in C++ sits on.

More like this

B
Build·beginner·10 min

Build your first web page

Hand-write a small, semantic page — a heading, a paragraph, a list — then open the same project in /code and make it yours.

B
Build·beginner·45 min·4 steps

Build a calculator

Most courses teach variables before you have anything to put in one. We are going to skip that pain. We build the calculator first; the concepts show up when the calculator demands them.

S
Build·beginner·12 min

Style a card with CSS

The quickest way to feel CSS: take a plain card and give it depth, spacing, and a button — editing the same project the Deep and Reference tiers use.

Liked this one?

Pass it on

Discussion

Be the first to ask

Your questions stay private with the author. The author can pin answers to share with everyone.

Sign in to ask a question privately on this tutorial.

Sign in

No pinned answers yet for this tutorial.