What is int main() in C++?

Short answer: int main() is the entry point of every C++ program — the function your operating system calls to start running your code. The int is the exit status it hands back when the program ends.

When you run a program, the operating system doesn't "just run your file". It loads the program, does some setup, and then calls a function that must be named exactly main — lowercase, spelled exactly like that. Whatever you put in main is what executes first. When main finishes, the program exits, and the integer you return becomes the program's exit status.

The smallest real program

Run it, then look at the line below the output — the playground reports the exit code it got back:

#include <iostream>
using namespace std;

int main() {
    cout << "The program is running." << endl;
    return 0;
}

What the return value actually does

The number you return goes back to the operating system. By convention, 0 means "everything went fine" and any non-zero value signals a problem — the specific number is yours to choose. Tools use this: build systems, test runners and shell scripts all check exit codes to decide whether the previous step succeeded.

#include <iostream>
using namespace std;

int main() {
    cout << "Pretending something went wrong..." << endl;
    return 42;
}

On your own computer you can see the same number: run the compiled program, then type echo $? (Linux/Mac) or echo %ERRORLEVEL% (Windows cmd) — the shell prints the code main returned.

The two standard signatures

The C++ standard allows exactly two forms:

int main()                        // no command-line arguments
int main(int argc, char* argv[])  // with command-line arguments

Do you have to write return 0;?

No — main is the one function in C++ that implicitly returns 0 if you fall off the end. Plenty of style guides still write it explicitly; both are correct. In every other function, forgetting a return on a non-void function is undefined behavior, so don't acquire the habit here.

Common beginner mistakes

Interview follow-ups that come up

  1. "Can main call itself?" Technically yes (recursion in main is legal, though bizarre); the program restarts its logic until the stack gives out.
  2. "Can main be overloaded?" No — the standard fixes its signature to the two forms above.
  3. "Where does the return value go?" To the process's exit status, visible to the parent process — shells, build tools, test frameworks.
  4. "Does anything run before main?" Yes: global/static object constructors run first. A favorite question for separating people who've read C++ from people who've debugged it.

Practice in the playground

  1. Write a main that prints your name and returns 7; confirm the exit code in the output panel.
  2. Break it on purpose: rename main to Main and read the error (in a real compiler it's a linker error, not a compiler error — worth knowing the difference).
  3. Add a second function, call it from main, and return its result.

Next: the code inside main usually loops — see C++ for vs while loops, and the line-by-line Hello World lesson.