Swipe
SwipeSwipe
Swipe Apps

Paths

Motivation

Information

C++ Program Structure · Lesson 10 of 25

The main() Function

A free lesson from Swipe Coding · C++, level 1 · updated October 2026

By the Swipe team, founded by Colin · how we make lessons

🤖
Every C++ program needs exactly one starting line — this is it. 🚀

Every C++ program needs a starting point — a place where execution begins. That starting point is one very specific function called main(). When your program is launched, the operating system looks for this function and starts running the code inside it.

#include <iostream>

int main() {
    std::cout << "Hello, C++!";
    return 0;
}
As soon as this program starts, execution begins inside the main() function.

The line "int main()" is called the function's signature. "int" means main() returns a whole number to the operating system when the program finishes — this number is called the "exit status" or "exit code". By convention, "return 0;" means the program finished successfully; any other number typically signals that something went wrong.

Quick check

What does the operating system look for to start a C++ program?

🤖
Zero means "all good" — worth remembering. ✅

You will often see "return 0;" as the very last line inside main(). This sends the number 0 back to the operating system, signaling that the program completed without errors. Some compilers will even assume "return 0;" for you if you leave it out of main() specifically, but it's good practice to write it yourself.

Fill in the blank

Execution of every C++ program begins inside the ____ function.

True or false

A C++ program can have more than one main() function.

🤖
Next up: what exactly counts as a 'statement' inside a function like main(). ➡️

Inside main(), you write your program's actual instructions — its statements — which run one after another, in order. We'll look closely at what a "statement" is in the next lesson.

Keep your streak going in Swipe Coding

Swipe through every level of C++ on your iPhone — with XP, stars, streaks, a personal advisor and the full track beyond these free levels.

Get the Swipe apps →