C / C++ FAQs & Programming Resources - ProkutFAQ : VoidMain

HomePage Recent Changes Recently Commented Login/Register

Revision [404]

Most recent edit made on 2007-06-21 11:25:18 by SharathAV

Additions:
The returned value from the main() goes to the host (ie. usually your operating system) which called your program. It indicates the exit status of the program to the host. Returning 0 means the program execution was successful, and returning any other value gives implementation defined status. The macro constants EXIT_SUCCESS and EXIT_FAILURE defined in <stdlib.h> can be used to indicate the programs termination status of success and failure respectively.


Deletions:
The returned value from the main() goes to the host (ie. usually your operating system) which called your program. It indicates the exit status of the program to the host. Returning 0 means the program execution was successful, and returning 1 means failure. The macro constants EXIT_SUCCESS and EXIT_FAILURE defined in <stdlib.h> can be used instead of the values 0 and 1 respectively. Meaning of returning any other value than 0 and 1 is implementation defined.




Revision [333]

Edited on 2007-03-03 13:57:57 by SharathAV

Additions:

In Unix-like systems, the returned value of the program is stored in a shell variable $?. So, after the execution of your program, issuing the command 'echo $?' would give you the returned value from main.
comp.lang.c FAQ: Can I declare main as void, to shut off these annoying "main returns no value" messages?


Deletions:
What is wrong with using void main()?
int main(void) { /* ... */ }
int main(int argc, char *argv[ ]) { /* ... */ }
Or any other form which is equivalent of the above two [such as int main() and int main(int argc, char **argv)].
In Unix-like systems, the returned value the program is stored in a shell variable $?. So, after the execution of your program, issuing the command 'echo $?' would give you the returned value from main.




Revision [331]

The oldest known version of this page was edited on 2007-03-03 08:51:16 by SharathAV

What is wrong with using void main()? Where does the returned value from main() go?


What is wrong with using void main()?
The main() function has to return an integer value which indicates the exit status of the program. The C and C++ standards only allow declarations for main function in the below forms:

int main(void) { /* ... */ }

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

Or any other form which is equivalent of the above two [such as int main() and int main(int argc, char **argv)].

So, declaring it as void main() or in any other form would make your program non-standard and non-portable. Some good compilers like gcc will issue a diagnostic and fail to compile when main function is improperly declared.

Where does the returned value from main() go?
The returned value from the main() goes to the host (ie. usually your operating system) which called your program. It indicates the exit status of the program to the host. Returning 0 means the program execution was successful, and returning 1 means failure. The macro constants EXIT_SUCCESS and EXIT_FAILURE defined in <stdlib.h> can be used instead of the values 0 and 1 respectively. Meaning of returning any other value than 0 and 1 is implementation defined.

In Unix-like systems, the returned value the program is stored in a shell variable $?. So, after the execution of your program, issuing the command 'echo $?' would give you the returned value from main.

References

Please don't void my main()!
void main(void) - the Wrong Thing
Valid XHTML 1.0 Transitional :: Valid CSS :: Powered by WikkaWiki
Page was generated in -0.7878 seconds