Thread: why int main?

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    7

    why int main?

    I am curious as to why we use int main, with a return 0 at the end. Is there anyone who just says
    Code:
    void main() {
    ...
    }
    without a return statement. Is there something better about int main?

  2. #2
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    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.

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by nonoob View Post
    T
    "void" is the old way... before things got standardized.
    I disagree about void being the old way.
    In the K&R C days there was no void type.

    Some people say "void main" is the embedded environment way of doing it.

    Tim S.

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    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

  5. #5
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    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()

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    88
    Quote Originally Posted by nonoob View Post
    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()
    Functions without an explicit type were implicitly declared to return int.

  7. #7

  8. #8
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Not enough coffee today. Sorry again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 06-01-2011, 03:08 AM
  2. [DEBATE]int main VS void main?
    By sudox in forum C++ Programming
    Replies: 20
    Last Post: 11-26-2010, 03:18 PM
  3. Why do people still use main() or main(void)?
    By Christopher2222 in forum C Programming
    Replies: 18
    Last Post: 06-06-2007, 06:34 PM
  4. Circular main <- main.o dependency dropped.
    By Queatrix in forum C++ Programming
    Replies: 4
    Last Post: 10-21-2005, 02:32 PM
  5. void main(), int main(), argc, argv[]????
    By Jonny M in forum C Programming
    Replies: 3
    Last Post: 03-06-2002, 09:12 AM