Thread: void main question..

  1. #1
    Banned
    Join Date
    Oct 2008
    Posts
    1,535

    void main question..

    i know that we discussed it before and i learned that we write main like this:
    Code:
    int main()
    {
    
    return 0;
    }
    but when i started my course i was told "the way to write main is":

    Code:
    void main()
    {
    
    }
    i was skeptic regarding what they says until i wrote it in visual studio 2005
    and it even didnt put out a warning

    whyyyy??

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >whyyyy??
    Visual C++ is one of the compilers that supports void main as an extension. It's safe on that compiler, but by using it you've removed any possibility of your code being portable.

    And for future reference, "it compiles for me" and "it runs for me" are not synonymous with "it's correct".
    My best code is written with the delete key.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by transgalactic2
    i was skeptic regarding what they says until i wrote it in visual studio 2005
    and it even didnt put out a warning
    Maybe the warning level was too low.

    EDIT:
    Oh yes, more like: maybe you did not disable language extensions.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Warning 2 warning C4326: return type of 'main' should be 'int' instead of 'void'
    But it really does require language extensions to be OFF, when they are ON (?) by default.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    Good programming practice would have main() return an int, at least. Main should return status upon exit. A good read on this would be

    http://www.eskimo.com/~scs/readings/...in.960823.html

  6. #6
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    I think part of the problem is that in Java, main() returns void, and people that switch back & forth between the languages a lot forget that main() returns an int in C/C++.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  7. #7
    Registered User
    Join Date
    Aug 2008
    Location
    Belgrade, Serbia
    Posts
    163
    What's to remember? main() returns int, simple as that.
    Vanity of vanities, saith the Preacher, vanity of vanities; all is vanity.
    What profit hath a man of all his labour which he taketh under the sun?
    All the rivers run into the sea; yet the sea is not full; unto the place from whence the rivers come, thither they return again.
    For in much wisdom is much grief: and he that increaseth knowledge increaseth sorrow.

  8. #8
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by hauzer View Post
    What's to remember? main() returns int, simple as that.
    Yeah, but then if you try that in Java you get an error.
    I have the opposite problem. I try to write int main() in Java all the time.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Not to mention the prevalence of poor compilers which implemented void main, back from the days of DOS (which was so lame at recognising an exit status, that no body bothered), and the piles of cheap pulp-fiction books which had titles such as "teach yourself code mangler C++ in 42 minutes".
    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.

  10. #10
    HelpingYouHelpUsHelpUsAll
    Join Date
    Dec 2007
    Location
    In your nightmares
    Posts
    223
    The OS returns an int from main, therefore using void main discards that value. Also on the off-chance that another program is monitoring it, using void main could cause problems. Anyway, compilers should at least support int main, if it doesn't, get anew one.
    The problem is clearly demonstrated in Salem's avatar.
    long time no C; //seige
    You miss 100% of the people you don't C;
    Code:
    if (language != LANG_C && language != LANG_CPP)
        drown(language);

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by P4R4N01D View Post
    The OS returns an int from main,
    Actually...
    The program returns something from main, to the runtime, to the OS.

    therefore using void main discards that value.
    Same here. The result is actually undefined. Your program can return whatever.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Well I think they should either make void main() illegal (a compile error) or define what it should do, such as alway returning 0.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  13. #13
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by cpjust View Post
    Well I think they should either make void main() illegal (a compile error) or define what it should do, such as alway returning 0.
    void main() *is* illegal, unless you have non-standard language extensions...

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  14. #14
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Well, int main returns 0 if you don't return something. So there is no sense for void main to do the same. It should be a compile error with the option to be enabled, since there might be some use of it, like in an embedded system where the value of main won't be checked.

  15. #15
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by C_ntua View Post
    Well, int main returns 0 if you don't return something. So there is no sense for void main to do the same. It should be a compile error with the option to be enabled, since there might be some use of it, like in an embedded system where the value of main won't be checked.
    But on platforms where the return value of main() isn't used, why doesn't the compiler just allow you to use int main() but throw away the return value?
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  2. Replies: 3
    Last Post: 05-13-2007, 08:55 AM
  3. Error, not sure what it means.
    By RealityFusion in forum C++ Programming
    Replies: 1
    Last Post: 09-25-2005, 01:17 PM
  4. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM
  5. what does this warningmean???
    By kreyes in forum C Programming
    Replies: 5
    Last Post: 03-04-2002, 07:53 AM