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
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.
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.
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.
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.
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....
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.