Chitransh Saurabh Chitransh Saurabh - 1 year ago 87
C Question

Return type of main function

Possible Duplicate:

What should main() return in C/C++?

Difference between void main and int main?

I have always been using the main method in C like

void main(){ // my code }

and it works pretty well for me.
I also know about the other
return type:

int main(void)
int main()
int main(int argc, char *argv[])

But I have not been able to find any resource that says that I can use
as a return type. Every book suggests that the return type must be
or else it be omitted. Then why does
void main()

Is this dependent on the version of C that I am using? Or does it work because I use a C++ IDE? Please reply specific to C and not C++.

Answer Source

Only book authors seem to be privy to the place where a return type of void for main() is allowed. The C++ standard forbids it completely.

The C standard says that the standard forms are:

int main(void) { ... }


int main(int argc, char **argv) { ... }

allowing alternative but equivalent forms of declaration for the argument types (and the names are completely discretionary, of course, since they're local variables to the function).

The C standard does make small provision for 'in some other implementation defined manner'. The ISO/IEC 9899:2011 standard says: Program termination

If the return type of the main function is a type compatible with int, a return from the initial call to the main function is equivalent to calling the exit function with the value returned by the main function as its argument;11) reaching the } that terminates the main function returns a value of 0. If the return type is not compatible with int, the termination status returned to the host environment is unspecified.

11) In accordance with 6.2.4, the lifetimes of objects with automatic storage duration declared in main will have ended in the former case, even where they would not have in the latter.

This clearly allows for non-int returns, but makes it clear that it is not specified. So, void might be allowed as the return type of main() by some implementation, but you can only find that from the documentation.

(Although I'm quoting C2011 standard, essentially the same words were in C99, and I believe C89 though my text for that is at the office and I'm not.)

Incidentally, Appendix J of the standard mentions:

J.5 Common extensions

The following extensions are widely used in many systems, but are not portable to all implementations. The inclusion of any extension that may cause a strictly conforming program to become invalid renders an implementation nonconforming. Examples of such extensions are new keywords, extra library functions declared in standard headers, or predefined macros with names that do not begin with an underscore.

J.5.1 Environment arguments

In a hosted environment, the main function receives a third argument, char *envp[], that points to a null-terminated array of pointers to char, each of which points to a string that provides information about the environment for this execution of the program (

Why does void main() work?

The question observes that void main() works. It 'works' because the compiler does its best to generate code for programs. Compilers such as GCC will warn about non-standard forms for main(), but will process them. The linker isn't too worried about the return type; it simply needs a symbol main (or possibly _main, depending on the system) and when it finds it, links it into the executable. The start-up code assumes that main has been defined in the standard manner. If main() returns to the startup code, it collects the returned value as if the function returned an int, but that value is likely to be garbage. So, it sort of seems to work as long as you don't look for the exit status of your program.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download