Thread: void main or int main

  1. #1
    Registered User subdene's Avatar
    Join Date
    Jan 2002
    Posts
    367

    Question void main or int main

    I see many examples or programs that start like this:

    Code:
    int main()
    {
      etc.
    
      return 0;
    }
    I've always been taught up to now to use the following code to start a main program.

    Code:
    void main()
    {
      etc.
    
    }
    Which is the best method to use? and why? thanks.

  2. #2
    Evil Member
    Join Date
    Jan 2002
    Posts
    638
    int.

    Use int.

    There is not any debate on this subject. The Standard has spoken.

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    479
    int main returns an int

    void main returns void
    it returns nada

    i guess they are good on different things

  4. #4
    Registered User
    Join Date
    Feb 2002
    Posts
    589
    Maybee I should clearify that

    void is wrong
    void will make other programmers hate you
    void will compromise the functionality of your program

    int is correct acording to the standard

  5. #5
    Registered User
    Join Date
    Feb 2002
    Posts
    589
    Originally posted by pode
    int main returns an int

    void main returns void
    it returns nada

    i guess they are good on different things
    NO void main() is horrible and absolutly not good at anything.
    void main() is the epidemi(sp) of evil.

    int main() will lead you to the light

  6. #6
    Registered User
    Join Date
    Mar 2002
    Posts
    27

    Why does it matter

    All you guys have said its bad, but not why...
    When I write a program with int main() it works the same as it does with void main(), but with void main I can skip the return statement...

    Gr3g
    Chance favors the prepared mind.

    Vis. C++ 6.0

  7. #7
    Registered User
    Join Date
    Feb 2002
    Posts
    589
    Well look at it like this

    If you drive on the wrong side of the road you will probably be fine as long as there isn't any other cars on the same road. It is the wrong side but it kinda works.

    If you use void main() the chances that it will work is pretty good as long as there isn't any other programs that will interact with your program. It is wrong but it kinda works.

    So please always drive on the right side of the road because it will pay of in fewer crashes and make other programers life safer. And this side of the road is just as fast but safer

  8. #8
    Registered User
    Join Date
    Jan 2002
    Posts
    559
    I'm not clear on the disadvantage either. The return is for the OS, lets it know the program executed ok. Or so I was taught.

  9. #9

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    1. Read this
    http://www.eskimo.com/~scs/C-faq/q11.12.html

    > It is wrong but it kinda works
    And so is dereferencing an uninitialised pointer. The only difference is, how long before you get bitten and your program stops working.

    > but with void main I can skip the return statement
    And if it skips something else as well, then you're stuck

    > The return is for the OS
    The return is to the calling environment, which could be the OS, but might be the shell or another program.

    If you ever plan to make programs which might be useful to others, you'd better start returning a meaningful status (even if for the moment, its always success). Then when it really matters, you won't have to think about it.

    I've had to deal with 'void main' programs in the past, and it gets real hard to figure out automatically whether such programs succeeded or failed, because the exit status is unpredictable (or useless).

    So do yourself and your future users a favour - return int

  11. #11
    Registered User
    Join Date
    Feb 2002
    Posts
    589
    > It is wrong but it kinda works
    And so is dereferencing an uninitialised pointer. The only difference is, how long before you get bitten and your program stops working.

    Think I didn't make my point clear enough. I claimed that there is only a matter of time before void main() crash and burn and someone gets bitten

  12. #12
    Registered User
    Join Date
    Sep 2001
    Posts
    305
    void returns nothing, so your OS doesn't know if your program had a successful run or not. it'll screw you over (ive had eperience).

  13. #13
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330
    The standard defines entry as:

    Code:
    int main (void)
    {
    That is all there is. main should enter this way and return a value.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  4. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  5. Quack! It doesn't work! >.<
    By *Michelle* in forum C++ Programming
    Replies: 8
    Last Post: 03-02-2003, 12:26 AM