Thread: [DEBATE]int main VS void main?

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    13

    [DEBATE]int main VS void main?

    I know the difference between these two setups, but I would like to start a debate thread here to see what people prefer and why. I've read tutorials that will write the main function as void and others that set it as int and it seems to vary based on person and language. So what is your opinion? I don't really have much of an opinion, however I always write my functions as int main due to force-of-habit; but I rarely ever write return 0;

  2. #2
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    There is no debate. The C++ standard requires main() to return an int.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    What pianorain said. No debate, period. It has NEVER been valid C++. So says the creator of C++.

    /end thread

  4. #4
    Registered User
    Join Date
    Nov 2010
    Posts
    13
    Ahh I wasn't aware of that, but why do other languages do it? It doesn't make sense... so say the programming Gods (C++ developers)

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Quote Originally Posted by sudox
    Ahh I wasn't aware of that, but why do other languages do it? It doesn't make sense... so say the programming Gods (C++ developers)
    Read what rags_to_riches linked to. Why is a value returned from the global main function in C++ in the first place? Is the usage of this return value universal? Are all programming languages used for the same purposes as C++?
    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

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by sudox View Post
    Ahh I wasn't aware of that, but why do other languages do it? It doesn't make sense... so say the programming Gods (C++ developers)
    The whole point is that a program, when it exits, may have need to return a status value to the environment it ran in. In a lot of environments (eg unix shells, windows command line interpreter, windows itself) the chosen way of doing that is to pass an integer value. The way chosen to return such an integer status in C (and hence C++, which is backward compatible to C in this regard) is by main() returning int.

    In C it is also possible to do that by calling exit(). In C++, that is also possible, but can have unwanted effects (eg not doing stack unwinding) so using exit() is not usually recommended.

    Designers of other programming languages choose to do things in different ways. Clearly, if the designer of some language deems there is no need to return program status to the host environment, there is no need for anything like int main() in that language.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  7. #7
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    497
    i think in the newest versions of compilers , there is no difference between using void or returning an int, cause if you happen to omit that return statement ! the compiler itself will add it!(at backstage)
    atleast mingw (gcc 4.5) does so !
    so to me sticking to the standards never hurt, so i just like this way .
    and also see this thread :
    why do we use int main() instead of void main?
    Highlight Your Codes
    The Boost C++ Libraries (online Reference)

    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.."
    Bill Bryson


  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    That you can omit return 0; in main() is really a separate issue. main either returns an int or it does not, and it is dependent on the return type, not the presence of a return statement.

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Masterx View Post
    i think in the newest versions of compilers , there is no difference between using void or returning an int, cause if you happen to omit that return statement ! the compiler itself will add it!(at backstage)
    atleast mingw (gcc 4.5) does so !
    so to me sticking to the standards never hurt, so i just like this way .
    and also see this thread :
    why do we use int main() instead of void main?
    void main is non-standard and thus should not be used. The fact that many compilers allow this is an err on their part, and it really sucks.
    The fact that you can omit the return statement is independent on the return type in this case. It is perfectly valid in C++ to omit the return statement in main, in which case an implicit return 0 will be added.
    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.

  10. #10
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Elysia View Post
    void main is non-standard and thus should not be used. The fact that many compilers allow this is an err on their part, and it really sucks.
    It's not actually an error in those compilers - I've yet to come across a C++ (or C) compiler that supports void main() without supporting the standard int main() variants. No compiler vendor can claim their compiler is standard compliant if it does not support int main().

    The C++ standard only requires that a compiler support the int main() entry points. It does not actually disallow other entry points (such as void main()).

    The error - and I agree it sucks - is actually with documentation that uses, or encourages use of, the void main() variants, with an incorrect claim that is standard. A few too many well-known compilers come with documentation that does this.

    The end effect, however, is that programmers who care about portability of their code - or not being seen as ignorant in forums like this one - will only use int main().
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  11. #11
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    When discussing this, you need to distinguish between a hosted and freestanding implementation. Freestanding implementations have much more freedom in how the program's entry point is specified.

  12. #12
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Regardless of whether an implementation is hosted or freestanding, it is required to support int main() and not required to support other entry points.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  13. #13
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    The point is that you can use void main and still be compliant if you're using a freestanding implementation that supports it. You're not even required to have a main function to begin with if your compiler supports it. It's not going to be portable, but that's not always a problem.

  14. #14
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Why use void main or even think of using it when the standard clearly requires int main? No debate here.

  15. #15
    Password:
    Join Date
    Dec 2009
    Location
    NC
    Posts
    587
    If you never intend to return, void main is just fine. Otherwise, you have no reason to use void main.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 08-16-2010, 10:00 AM
  2. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. i cannot see the mouse arrow
    By peachchentao in forum C Programming
    Replies: 6
    Last Post: 12-10-2006, 04:14 AM
  5. need help with handelling multiple source files
    By DarkMortar in forum C++ Programming
    Replies: 38
    Last Post: 05-26-2006, 10:46 PM