Short answer
int main() is the function where a C++ program starts running. The name main is required, int says it hands back a whole number when it finishes, and the empty () says it takes no inputs. The return 0; inside it tells the operating system that the program ran successfully.The short version, piece by piece
Here is the smallest program most beginners see:
#include <iostream>
int main() {
std::cout << "Hello from main\n";
return 0;
}
| Part | What it means |
|---|---|
int | The type of the value main gives back. It must be a whole number. |
main | The name. Every C++ program must have a function called main, all lower case. |
() | The list of inputs. Empty here, so main takes none. |
{ ... } | The body. The statements inside run in order, top to bottom. |
return 0; | Ends main and reports the value 0 to the operating system. |
Why main is special
Learn C++ states that every C++ program must have a special function named main, and that a program without one will fail to link. When you run the program, the statements inside main run one after another in sequential order. Most statements end with a semicolon, and leaving one out gives a syntax error.
cppreference describes main as the designated start of a program. It also lists some things that are not allowed with it: you cannot call main yourself from inside your own code, and you cannot take its address. Both make the program ill-formed.
Your other functions only run if main calls them, directly or through another function. That is why beginner tutorials put everything inside main first.
What int means here
int is the return type. A function can hand a value back to whoever called it, and for main the caller is the operating system. The value is a whole number.
cppreference is strict about this: the return type of main must be int. It also says the return type cannot be deduced, so auto main() { ... } is not allowed.
What return 0 does, and whether you need it
Learn C++ explains that return 0; sends a value to the operating system to say whether the program ran successfully. Returning 0 means everything went okay. It is the last statement that runs.
You can leave it out of main. cppreference says that if control reaches the end of main without a return statement, the effect is the same as executing return 0;. This program is valid:
#include <iostream>
int main() {
std::cout << "No return statement here\n";
}
That special rule applies only to main. Other functions that return a value need their own return.
Many people still write return 0; to make the intent obvious. cppreference adds that returning from main is equivalent to leaving the function normally and then calling std::exit with the same value.
Reporting failure
The same cppreference page uses two names in its example, EXIT_SUCCESS and EXIT_FAILURE, to return a result from main. They come from the <cstdlib> header. Here is a small program that uses both:
#include <cstdlib>
#include <iostream>
int main() {
int apples = 3;
if (apples < 5) {
std::cout << "Not enough apples\n";
return EXIT_FAILURE;
}
std::cout << "Enough apples\n";
return EXIT_SUCCESS;
}
On our test machine, a command shell reported exit status 1 after this ran. Do not rely on that exact number across systems; use the named constants.
The two allowed forms
cppreference lists two standard forms of main:
int main() { /* body */ }
int main(int argc, char* argv[]) { /* body */ }
The first ignores any command line arguments. The second receives them: argc is how many there are and argv holds them as text. This program prints them:
#include <iostream>
int main(int argc, char* argv[]) {
std::cout << "argc is " << argc << '\n';
for (int i = 0; i < argc; i++) {
std::cout << "argv[" << i << "] is " << argv[i] << '\n';
}
return 0;
}
When we ran it as a3.exe one two on Windows, it printed argc is 3. The first entry, argv[0], was the path of the program itself, followed by one and two. As a beginner you will use the first form for a long time.
cppreference also allows an implementation-defined third form, as long as it returns int. Treat that as an advanced detail.
Why void main() is wrong
Many old examples online write void main(). By the rule above, main must return int, so void main() is not a valid form in C++. We tested this with GCC 16.2 (MSYS2 build) on Windows:
#include <iostream>
void main() {
std::cout << "Hello\n";
}
It refused to compile with error: '::main' must return 'int'. Other compilers may behave differently, and we did not test them, so the safe habit is simple: always write int main().
Common beginner mistakes
- Writing
MainorMAIN. The name is all lower case. - Forgetting the semicolon after
return 0. - Calling
main()from your own code. It is not allowed. - Putting
return 0;in a function that returnsvoid. That is a different error. - Writing
auto main(). Not allowed either.
Practise reading real lines
Knowing the shape of main is the first step. The next is reading lines inside it and predicting what changes. For that, try the beginner learning plan, or see why some mistakes in main crash a program in what a segmentation fault is. If you wonder how steep the climb is, read is C++ hard to learn.
Teaches C++ to complete beginners as a game where you tap one real line of code at a time.
Get it on Google PlayFree to play. Halls 1 to 4 are free with no time limit, and free players can watch optional rewarded video ads for bonuses. Segfault Pro, a monthly subscription or a 3-month pass paid once, opens hall 5 and up, unlimited practice sets and mock exams, with no ads.
Segfault is a game that teaches C++ to complete beginners. You never type: each card shows a box and a target number, and you tap the one real line of C++ that makes them match. Every line is compiled and run with a real C++ compiler. Halls 1 to 4 are free; Pro opens hall 5 and up. It will not make you a programmer on its own, because you tap lines rather than write them.