What should main() return in C++? Comparison with C and standards

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Introduction: The main question even pros ask

\n\n

Have you ever wondered why every C++ program starts with int main() and not void main()? And what actually happens when you write return 0? This question is a real hit on Stack Overflow, gathering thousands of views and hundreds of comments. It seems like a basic topic, but it hides a whole layer of subtleties that separate C and C++, as well as beginners and experienced developers.

\n\n

In this article, we won't just give a dry answer. We will conduct a comparison of approaches in C and C++, analyze the standards, show real code examples, and explain how to correctly work with the return value of main() so that your code is not only functional but also portable. Whether you are learning C++ or write in it every day, this article is for you.

\n\n

Short answer: What should main() return in C++?

\n\n

If you are in a hurry, here is the essence: the main() function in C++ must return int. This is a strict requirement of the standard (from C++98 to C++23). The return value tells the operating system whether the program completed successfully.

\n\n

Here is the minimal correct code:

\n\n
#include <iostream>\n\nint main() {\n    std::cout << "Hello, World!" << std::endl;\n    return 0; // Successful completion\n}
\n\n

But this is just the tip of the iceberg. Let's dig deeper.

\n\n

Why int and not void? Historical background

\n\n

In the earliest versions of C (before the ANSI C standard), the type of main() was not strictly defined. Many compilers allowed writing void main(), and it worked. However, with the advent of standardization (C89/C90 for C and C++98 for C++), it was decided that main() should return an exit code to the operating system.

\n\n

Why? Because scripts, Makefiles, and other programs often check how your process terminated. Return code 0 means "success," and any non-zero value means "error" (or a specific type of error). If main() were void, you wouldn't be able to pass this information.

\n\n

In C++, the situation is even stricter. The C++ standard (section [basic.start.main]) directly states: main() must have the type int. Any other declaration (for example, void main()) is a compiler extension and makes the code non-portable.

\n\n

Comparison of C and C++: What's the difference?

\n\n

Although both languages require int main(), there are several key differences:

\n\n
    \n
  • Implicit return 0 in C++: If you don't write a return at the end of main(), C++ automatically inserts return 0. In C (starting from C99) this also works, but in older standards (C89) the absence of a return led to undefined behavior.
  • \n
  • Command-line arguments: In C++, you can declare int main(int argc, char* argv[]) or int main(). In C, there is also a third variant with the environment (char* envp[]), but in C++ it is not standardized (although many compilers support it).
  • \n
  • void main(): In C, some compilers (for example, for embedded systems) may allow void main(). In C++, this is a gross error, and the code will not compile in strict mode.
  • \n
\n\n

Example for C++ (always works):

\n\n
int main() {\n    std::cout << "C++ will automatically return 0" << std::endl;\n    // return 0; // can be omitted\n}
\n\n

Example for C (requires explicit return in older standards):

\n\n
#include <stdio.h>\n\nint main() {\n    printf("In C89 you need to explicitly return 0\\n");\n    return 0; // mandatory\n}
\n\n

What do return codes mean? Breakdown with examples

\n\n

The return code is a

Blogs

Book Recommendations