return value of main() [was: need some explanation about this..]
hi everyone!
i have been using void main() since long now...but i saw that some programmer says that it's not good, instead shud use int...
plz can anyone explain why??
since i dnt want main to return anything, i used void, but y not void??
tnx for your attention!
[295 byte] By [
wakish] at [2007-11-11 6:55:02]

# 1 Re: return value of main() [was: need some explanation about this..]
If you want your program portable, you must use int main(). Because some compiler will complain you if you use void main() (g++).
An why you should return value from main ?? That's because OS expected that return value to determine whether that your application exit normally or not.
# 2 Re: return value of main() [was: need some explanation about this..]
Int main is the only acceptable way according to the standard.
its a relic from unix, which dos adopted, that the os gets zero if the program exited
safely and other numbers if there were errors. Can be handy for debugging.
Note that windows supports others -- I have made a double main that I used inside a matlab program -- but this is exploiting the extended support and still nonstandard.
jonnin at 2007-11-11 21:03:51 >

# 3 Re: return value of main() [was: need some explanation about this..]
int main() is the Unix standard and also ANSI standard.
void main() is nonstandard.
NabeeL at 2007-11-11 21:04:49 >

# 4 Re: return value of main() [was: need some explanation about this..]
ok...i c..
apart from "return 0" , is there any return value for int main() ??
wakish at 2007-11-11 21:05:50 >

# 5 Re: return value of main() [was: need some explanation about this..]
any integer is legal. 0 means 'exited ok' while any other number means 'error'
jonnin at 2007-11-11 21:06:49 >

# 6 Re: return value of main() [was: need some explanation about this..]
hey jonnin i have tried to search a bit, and i found out that some people use EXIT_SUCCESS as main's return value. It is defined in the <cstdlib>.
I learned that this makes for more readable and descriptive code when compared to returning a value such as 0.
What's your opinion about this dude?
Tnx for your attention,
Kind Regards!
wakish at 2007-11-11 21:07:59 >

# 7 Re: return value of main() [was: need some explanation about this..]
Renaming a constant to explain what it is makes for readable code. I am all about doing that! I also like to tie constants to whatever they are used for with a namespace or class membership so they are "bound" to the stuff that uses them instead of leaving the reader wondering what all will be broken if the constant is changed (recompile of course).
jonnin at 2007-11-11 21:08:57 >

# 8 Re: return value of main() [was: need some explanation about this..]
yeah, that's right indeed..tnx!
wakish at 2007-11-11 21:09:56 >

# 9 Re: return value of main() [was: need some explanation about this..]
first of all, you don't really have to return anything explicitly from main. When there's no explicit return statement, the compiler implicitly inserts a return 0; at the end. 0 is an agreed-upon code for success. You can return any other value between 1-127 to indicate a failure of some sort. This technique is used by launchers or parent processes that check the return status of their child process. EXIT_SUCCESS and EXIT_FAILURE are standard constants that you can use to make the code more readable but as I said, EXIT_SUCCESS is redundant.
As for the prototype: void main() has never been a standard form. In earlier versions of C, it was just main() which implies int main() (the famous defualt to int rule).
Danny at 2007-11-11 21:10:53 >
