1. Summary
  2. Files
  3. Support
  4. Report Spam
  5. Create account
  6. Log in

Void main

From cpwiki

Jump to: navigation, search

Standard issues

C and C++ programs that use void main() are non-standard. Except in some special cases like embedded systems, void main() should not be used. The proper return value for main is int, not void. Thus, main() should be declared like so:

int main()

Actually, main can be declared in any of the four following ways:

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

Unless a program uses command-line arguments, one of the first two would suffice.

Practical issues

Not only is void main() not standard, using it can cause problems when the return value of the program is examined, which is a common occurrance in situations like shell scripting. The return value of a program is often used to check whether a program "failed" or "succeeded". With int main(), one typically returns 0 to indicate success, and other numbers to indicate various errors. With void main() an undefined number is returned, so anything checking the return value of such a program will probably think an error occurred, because a random integer is far more likely to be nonzero than zero.

Acceptable use

It should be noted that some implementations of C, usually for embedded systems or other platforms with limited resources, require void main(). These implementations are not ISO-standard-conforming, however. Usually this is because implementing standard C would be a waste of effort or resources on these platforms, which have their own interfaces.

Personal tools