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 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;
}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.
What does the operating system look for to start a C++ program?
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.
Execution of every C++ program begins inside the ____ function.
A C++ program can have more than one main() function.
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 →