I am curious as to why we use int main, with a return 0 at the end. Is there anyone who just says
without a return statement. Is there something better about int main?Code:void main() { ... }
I am curious as to why we use int main, with a return 0 at the end. Is there anyone who just says
without a return statement. Is there something better about int main?Code:void main() { ... }
That's the "new" standard. Making main() be a function that returns int means we can return an integer... to the calling program which is implied to be whatever shell or ancestor process called it.
"void" is the old way... before things got standardized. It was thought that the C program is stand-alone and no one calls it except the manual execution by the user.
I'm not going to bother to look up specific dates of standards - you can do that if you like.
Perhaps some embedded systems still accept void without issuing warnings/errors because such systems don't have a whole lot of operating system overhead that wait for some static code to be returned. They just run and run.
Last edited by nonoob; 12-15-2011 at 12:46 PM.
The standard HAS NEVER specified main as returning void. Anything other than int is non-standard. If you want you can read this from Mr. Stroustrup.
That said, there are plenty of people who use void main and it may be accepted by your compiler but you should always stick to the standard as much as possible.
"Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
-Christopher Hitchens
I stand corrected. void function type and void parameter instead of empty parenthesis was added later. I'm remembering old C compilers of the 80s where one would just do main()
cprogramming.com is there to help you
FAQ > main() / void main() / int main() / int main(void) / int main(int argc, char *argv[]) - Cprogramming.com
Not enough coffee today. Sorry again.