Can't the main function be of type 'void'?

It seems that the main function can not be of "void" type. Is it true?

If so, why C++ prevent the type of the main function to be void?

Thank you very much!
Last edited on
Well, technically it "can" be. It's against all standards and morals though

Just don't do anything but int main
Last edited on

Thanks for your reply. I tried something like

void main(){
// no return at the end

Do you know why this does not compile? I am bounced back by the following error:

'main' must return 'int'
Last edited on
Then you have a good compiler. What are you using? Anything relatively recent should keep you from doing that. Basically, your OS invokes main to start your program, and that return value? Your OS is typically expecting that to know whether or not the process ended normally
Hi ResidentBiscuit,

Thanks. I am using g++, but I don't know the exact version. Do you know how to get the detail about the compiler at the command line/console/terminal?
Are you on windows? If so just...
g++ -v

It's probably similar on Linux or Mac, but I dont know
Hi ResidentBiscuit,

It works. My cpp compiler is gcc version 4.1.2 20080704 (Red Hat 4.1.2-48). Thank you!
using vc++ 2010 professional.

void main()

compiles just fine for me
@Nexius that's a non-portable language extension offered by Microsoft's compilers. Pretty much every compiler offers some number of language extensions, and it's important to distinguish those from the standard language.
Yup, standards say main should only be of type int
compiles just fine for me

I drove a car this morning and nobody died, therefore all cars are completely safe and nobody ever died or was injured in a car-related event.
Hmm, I'd say someone is now reporting everyone... Wonder who that would be
The reason, besides it being a standard, goes back to a time before MS Windows, when C was developed for use on and for UNIX. Often full programs were little more than a string of utilities, each doing some smaller part of the whole glued together with shell scripts, and the utils needed to return a value indicating failure or success in some way that the shell script could receive.

The "int main(int argc, char *argv[])" became standardized for C and subsequently C++, though MS does it's best to remake the mold.
Topic archived. No new replies allowed.