void main() and int main()

Hi,

Just wondering as a beginner in C++ programming, what is the difference between using the code void main() and int main()?
In a hosted implementation (the kind of implementation that we use), there is no void main() { /* ... */ }

See: http://www.stroustrup.com/bs_faq2.html#void-main
void main() is a c thing
in c++ we use int main()


main is a function just like any other function void / int is its return value. The integer we return in c++ is used to indicate to the operating system whether main executed successfully or not

1
2
3
4
5
int main()
{
    //....
     return EXIT_SUCCESS;
}
void main() is a c thing
A common misconception. C never allowed void main in hosted enviroments either.
You are right, however as any implementation defined overload it is not guaranteed to work outside of specific implementation, which limits portability.


I'll correct my statement to "void main() is not guaranteed to work in all conforming implementations"

BTW, why the termination status returned to the host environment is unspecified? Why not implementation-defined, like it should logically be?
IDK. I don't wanna look it up right now, but I think it's somewhere on gotw of an example where void main caused a serious malfunction in the host environment. (I think that is arguably an error in the compiler, though.)

I'm not sure that logic really mandates it be more correct as implementation-defined. Being unspecified really only means that the compiler gets to decide how some mandated thing is done without saying exactly how it will do it. This gives compiler writers some leeway when dealing with odd environments.
Fortunately, this topic is not too old for me to have found the void main() example of horrors:

https://www.ty-penguin.org.uk/~auj/voidmain/

The legality of void main() is not so important; C++14 specifically states that it is invalid; in implementations prior to that it was non-portable, non-conformant, and dangerous.

main() returns int.
Topic archived. No new replies allowed.