void main()

When voidcan be instead of int ? I mean void main()
There is a programmer who writes that, and do not understand how does it work?
(click on the first one source in the list) - http://codeforces.com/contest/500/status/A
Last edited on
closed account (EwCjE3v7)
You wouldn't want to use void main() because many compilers will give you an error.

but if void main was to be accepted then there would be no return type, UNIX like systems will use what's returned from main for checking if the program succeeded or failed, return -1 means fail while 0 means everything went okay.

And that code you viewing is probably really really old.

And your just not supposed to use void main(), it won't be accepted many times.
Well unless you're using an unhosted system (no operating system), main() should be defined to return an int, and you should return an int from that function.



> return -1 means fail while 0 means everything went okay.
0 means everything went okay
everything else indicates an error
Never return negative numbers from main().
If you want to be especially careful, don't return anything greater than 255.

And in C90, void main() is actually legal.
http://homepage.ntlworld.com/jonathan.deboynepollard/FGA/legality-of-void-main.html

And in C99 it is permissible in some implementation defined contexts.

In C++ it is expressly forbidden. All modern compilers I am aware of (which isn't saying much) will complain if you use void main(), but some will accept it still.
closed account (EwCjE3v7)
@Duoas why not? I've been using -1 for a long time and nothing is wrong with it.
I've been using -1 for a long time and nothing is wrong with it.
...on your system.

Either your compiler is cropping the value for you or you are managing to avoid some additional (negative) information that the high bits indicate when set in the return value.

Hope this helps.
Funny, I've almost always used void main () so I don't need to worry about returning anything. Never had a problem on my system nor the class computers (I started doing that since my second version of Hello World in class)..

I've used int main a couple times, but generally so I can have a split path exit main early.
Even in the C90 days, using "void main()" was considered very poor practice. It takes one line of code to ensure that your program returns a value that isn't ridiculous. Just do it... if you don't know what to return, just return 0 always. If you care to signal a failure, return 1 - 255. This is pretty much an assumed idea that all programs do this since if anything executes your program, it can determine if it failed or not by the returned value.

I think it would have been okay if "void main()" implicitly returned a value of 0 but it complicates startup ever so slightly so virtually no compiler that I know of does this and returns junk instead (gcc returns 182, VC I think literally returns junk, not sure about PCC or TCC). D on the other hand think it's fine and people use "void main" in D with no issues, properly returning 0.
If you care to signal a failure, return 1 - 255.

Actually for greatest portability, you should use the macros EXIT_SUCCESS and EXIT_FAILURE or zero. On Unix/Linux machines the limit is 0 - 255, Windows however appears to accept much larger values, to the maximum value allowed for a signed 32 bit int.

Herm... I'd be okay with that if there wasn't information associated with the return values. For instance, 2 might mean a different error occurred than if 3 is returned.

Er, unless you mean you should check for maximum size of exit value usable... in which case 1-255 is still a pretty good safe area....
Last edited on
Ah. found: http://stackoverflow.com/questions/13667364/exit-failure-vs-exit1

Learned something new today...
With strict ISO C90 conformance enabled (std=c90, -pedantic-errors),
both clang and GCC generate an error for void main()

With just -pedantic, clang gives the same error: 'main' must return 'int'
gcc passes the code with a diagnostic: return type of 'main' is not 'int'

http://coliru.stacked-crooked.com/a/33d1d0d4f32b55c6
Er, unless you mean you should check for maximum size of exit value usable... in which case 1-255 is still a pretty good safe area....

For Unix/Linux this is true. As I said however Windows allows much larger values to be used, up to 65536. Anything other than EXIT_SUCCESS or zero and EXIT_FAILURE are implementation defined.

You may want to look at this link for Windows:
http://msdn.microsoft.com/en-us/library/ms681381.aspx
And this link for Linux using gcc
http://www.gnu.org/software/libc/manual/html_node/Exit-Status.html
> Windows allows much larger values to be used, up to 65536

AFAIK, Windows allows any 32-bit unsigned value other than 259 as the exit status.
The GetExitCodeProcess function returns a valid error code defined by the application only after the thread terminates. Therefore, an application should not use STILL_ACTIVE (259) as an error code.
http://msdn.microsoft.com/en-us/library/windows/desktop/ms683189(v=vs.85).aspx
Topic archived. No new replies allowed.