Thread: " void main() is evil. Use int main(). "

  1. #1
    Registered User
    Join Date
    Mar 2007
    Location
    India
    Posts
    17

    " void main() is evil. Use int main(). "

    " void main() is evil. Use int main(). "
    i read this message in the forum. but what is the main reason behind this ?
    say i do void main() in my programme then, if i am giving the scope for some other programme to take out any value from my programme ? or any thing different.
    what is wrong in void main() ?

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    It is non-standard and not supported by all compilers. Do you have a problem with typing one character less? (main doesn't need the return statement, 0 is returned implicitly)
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    Jun 2007
    Posts
    7
    hi, I'm very new to c++ but always though int main() was so return(0) would return a 0 to the calling application..

    Can't I return(1); to drop an error message from my application?

  4. #4
    Massively Single Player AverageSoftware's Avatar
    Join Date
    May 2007
    Location
    Buffalo, NY
    Posts
    141
    Quote Originally Posted by cope View Post
    hi, I'm very new to c++ but always though int main() was so return(0) would return a 0 to the calling application..

    Can't I return(1); to drop an error message from my application?
    Yes you can (and should). If you ever run your application from a script, it's very important to be able to test for success.
    There is no greater sign that a computing technology is worthless than the association of the word "solution" with it.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Which part don't you understand?
    http://faq.cprogramming.com/cgi-bin/...&id=1043284376
    http://users.aber.ac.uk/auj/voidmain.shtml
    http://c-faq.com/ansi/voidmain.html
    http://c-faq.com/ansi/voidmain3.html
    http://c-faq.com/ansi/voidmainexamp.html
    http://c-faq.com/ansi/voidmainbooks.html

    > say i do void main() in my programme then, if i am giving the scope for some other programme to take out any value from my programme ?
    a) "works for me" is never a defence. Just because your compiler allows it, or because you don't check the result doesn't make it OK.
    b) assuming for the moment that your program "works" and is deemed useful enough for someone else to use, they may find that the return result of your program is not something useful like 0 for success or 1 for error, but instead some random garbage bearing no resemblance to what actually happened.

    Just so you know, I have direct personal experience of such a program written by a void main programmer. That was a definite "voodoo doll" moment.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Jul 2006
    Posts
    162
    There's a host of very good reasons to why standards exist and why we abide, including the potential consequences for not. Use them, whether you believe you need them or not.

    http://en.wikipedia.org/wiki/Standard

    And to add, the most frustrating thing about beginners is they don't understand a simple concept that just because you don't need it -now- doesn't mean you won't need it -later-. If you develop habits -now- it's not a -problem- later.

    If you post non standard code you will always be corrected, and if you ever complain, it won't stand the test of time and reason.

    Enjoy!
    Last edited by simpleid; 06-06-2007 at 11:41 AM.

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    There are literally hundreds of threads about declaring main(). Here's one of them: http://cboard.cprogramming.com/showthread.php?t=86152

    Prelude's post there also explains how int main() implicitly returns 0.

    [edit] In fact, the most recent thread in the C programming forum is about main(). You can have a look at it for a C perspective: http://cboard.cprogramming.com/showthread.php?t=90565 [/edit]
    Last edited by dwks; 06-06-2007 at 11:57 AM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Here is a post I wrote on the subject of void main() with regard to C. Consider the programs and their output as proof-of-concept (and also in need of being updated ).

    For those that just started programming, I wouldn't concentrate on details like this. Know that there are answers as to why we stress the need for main() to be declared to return an int, and just do it.

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. My graphics library
    By stupid_mutt in forum C Programming
    Replies: 3
    Last Post: 11-26-2001, 06:05 PM