What's the difference between int main() and void main()?
What's the difference between int main() and void main()?
The type prior to main tells the compiler what type of return value the function will be returning.
In the case of the special function "main", it should always return a value to the caller to let the caller know the success or failure of the process.
"int" says an integer will be returned, commonly called a "return code".
"void" says no value will be returned.
"void main" should be avoided. It's "old school".
Mainframe assembler programmer by trade. C coder when I can.
Any case where a beginner might want to use void main() or main(void) (difference?) instead of int main()
Void main is wrong and indeed has almost never been right (though I remember a compiler I used for an embedded machine required void main()).
One out of every five times you use int main() you will find a dollar. Every time you use void main() you will drop a dollar for an int main() user to find... Sorry... rules are rules.
>> Any case where a beginner might want to use void main() or main(void) (difference?) instead of int main()
http://www.research.att.com/~bs/bs_faq2.html#void-main
If you're not sure, use int main.
Glauber,
The programing language standard says that main() should return an int. It's one of the many rules in C++.
Have you studied functions* yet? When you study functions, you will learn the difference between void SomeFunction() and int SomeFunction()
When your program calls a function, the function can return a value (of a particular type) when the function is done. A return-type of void in the function prototype indicates that no value is returned. Otherwise, the return value can be the result of a calculation (done by the function) or it can be a pass/fail flag, or anything else.
In the case of main(), you may be able to "get away" with the incorrect practice of returning nothing (if your compiler allows it), because main() doesn't return it's value 'till your program ends... So, by the time the operating system sees the error, your program is no longer running...
* The cprogramming.com tutorial just scratches the surface. You'll need a book (or other resources) to really learn about functions.
Last edited by DougDbug; 05-27-2008 at 07:29 PM.
There is one "special" thing about main() (in C99), in that if the closing } is reached without ever seeing a return statement, 0 is returned.
--
Computer Programming: An Introduction for the Scientifically Inclined
This is C++ though, so C99 does not matter, but it also happens that C++ has the same rule.There is one "special" thing about main() (in C99), in that if the closing } is reached without ever seeing a return statement, 0 is returned.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Thanks.