Why no return statement for int main()?

I just learned about functions in the C++ Fundamentals course.
He explains that functions with a return type, like int, need a return statement.
However, int main() does not have one.
How does this work?

There isn’t an “int main()” in the code.

int main{1} would specify a new integer (i.e. a whole number (1, 2, 3) and not a float (1.0, 2.0, 3.0) with a name of “main” and a value of 1, which isn’t the same as main(). That’s a function called at startup.

I’ve just started learning, probably a better answer than this!

https://en.cppreference.com/w/cpp/language/main_function

Actually, that’s not quite right.

The main function is supposed to return a value, with the integer returned being a code to give the user some context of the result of running the function. return 0 being the response for a program exiting with no errors.

Except… we programmers are lazy, so instead of writing return 0 every time we made the compiler auto-insert it when building our code if a return statement is not supplied.

Note, this is ONLY for the main function. Since that function is present in every program and is therefore predictable in its use and outcome.

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms