int main () and void main ()

This is a discussion on int main () and void main () within the C++ Programming forums, part of the General Programming Boards category; im interested which is better why it is better, and when to use the other one. ofcourse i googled, but ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    86

    int main () and void main ()

    im interested which is better why it is better, and when to use the other one.

    ofcourse i googled, but lots of words i dont understand ( specific functions etc ) that pop out in the explaination on those pages makes it unclear for me what it actually says.

    so im asking here :P for a simple explaination.

  2. #2
    For Narnia! Sentral's Avatar
    Join Date
    May 2005
    Location
    Narnia
    Posts
    719
    Always use int main(). I think void main() gives a warning. Or you can use int main(void)
    Videogame Memories!
    A site dedicated to keeping videogame memories alive!

    http://www.videogamememories.com/
    Share your experiences with us now!

    "We will game forever!"

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    86
    it doesnt give a warning when i use it in my programs... lots of tutorials use void main..

    so thats wy i asked. actually i was looking at someones avatar on this forum about int main and void main (getting blown up) and asked myself why void main is "bad"?

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    903
    If I recall, void main() used to be acceptable in C until a newer standard. This is because (correct me if I'm wrong) a program needs to return a value to the operating system when it quits. void main() implies that the program doesn't return a value to the operating system.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,299

    Cool

    void main() has never been valid C or C++!

    Read here: http://www.research.att.com/~bs/bs_faq2.html#void-main

  6. #6
    Mad OnionKnight's Avatar
    Join Date
    Jan 2005
    Location
    Umeň, Sweden
    Posts
    555
    void main () is always wrong. The function that calls your main() expects it to return an int and has no idea what the main() you made actually returns. The problems start when your function returns nothing and the caller goes ahead to retrieve what was returned. What will be retrieved? If it fetches the return value from a register it will probably just get something strange and random. If it gets it's value from the stack all hell will break loose.

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    While void main has never been allowed in standard C or C++, it has been allowed by some compilers. Some compilers (most notably VC++) will not necessarily even give a warning even though it is not technically allowed by the standards.

    Using void main, despite being "incorrect" is really not a big deal in and of itself if your compiler allows it. It is such a big issue because use of void main indicates that either the programmer is ignorant of standard practice (possibly because he or she is new to programming and has only been exposed to incorrect resources or because he or she has been programming since before int main was standardized and has not yet been exposed to the newer information), or the programmer chooses to ignore standard practice (often due to laziness or a fascination with breaking the rules). Either way, it does not reflect well on the programmer and perpetuates the misinformation and/or attitudes that lead to the continued use of the incorrect practice.

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,485
    > so im asking here :P for a simple explaination.
    So have you read the FAQs yet?
    http://faq.cprogramming.com/cgi-bin/...&id=1043284376
    http://c-faq.com/ansi/maindecl.html
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    Registered User
    Join Date
    Jun 2005
    Posts
    6,252
    Quote Originally Posted by Desolation
    If I recall, void main() used to be acceptable in C until a newer standard. This is because (correct me if I'm wrong) a program needs to return a value to the operating system when it quits. void main() implies that the program doesn't return a value to the operating system.
    Consider yourself corrected. As iMalc said, void main() has never been valid C nor C++. It wasn't valid before those languages were standardised, and the standards have not made it valid.

    Even the early versions of C (eg K&R C, which predated standard C by well over a decade) required int main(). The 1989 C standard, the C++ standard (which was required to be backward compatible to the 1989 C standard), and the 1999 C standard all required that any compliant compiler must support two forms of main(): "int main(void)" and "int main(int argc, char *argv[])" or functional equivalents. The C and C++ standards say nothing about what happens to the return value (eg being returned to an operating system): the only statement made is that "return value;' from main() has the same effect as calling "exit(value);".

    Now, why do some text books, tutorial examples, and teachers incorrectly tell you "void main()" is standard? That's history..... void main() was a compiler-specific extension supported by some compilers. Microsoft compilers were (and still are, IIRC) among those and some other compilers, which targeted MS-DOS or windows development, supported the same extension. Quite a few tutorial examples, and a number of text books, were written by people who only used Microsoft compilers, and those people believed void main() was standard because Microsoft documentation claimed it was standard (in some cases, authors of those tutorials and text books were Microsoft employees). Some text books went so far as to provide examples which used Microsoft specific libraries (eg user interface libraries).

    There were side issues in the history as well: Microsoft was strategically actually quite aggressive up until the mid 1990s, in claiming, in their marketing material, that their C and (particularly) C++ compilers were standard compliant while, technically, they deliberately included non-standard features and implemented things in non-standard ways. The reason for this was simple: the marketing encouraged people to use their compilers in the belief they were standard compliant and their documentation and compilers encouraged developers to use non-standard techniques, and this resulted in vendor lock-in. Fortunately that strategy changed and, with Visual C++ Version 7 and later compilers, Microsoft sought to make the compilers legitimately standard compliant. The problem is that older Microsoft compilers (version 6 and earlier) are quite notable in the lack of compliance to standards.

  10. #10
    Registered User
    Join Date
    Nov 2006
    Posts
    86
    ok , that clears it all up for me, ty guys

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,598
    Consider yourself corrected. As iMalc said, void main() has never been valid C nor C++. It wasn't valid before those languages were standardised, and the standards have not made it valid.

    Even the early versions of C (eg K&R C, which predated standard C by well over a decade) required int main(). The 1989 C standard, the C++ standard (which was required to be backward compatible to the 1989 C standard), and the 1999 C standard all required that any compliant compiler must support two forms of main(): "int main(void)" and "int main(int argc, char *argv[])" or functional equivalents. The C and C++ standards say nothing about what happens to the return value (eg being returned to an operating system): the only statement made is that "return value;' from main() has the same effect as calling "exit(value);".
    I cannot find the online reference to an article on this, but someone once pointed out that the C standard (C99, I think) contained a 'bug' (i.e., inaccurately phrased language) that unintentionally made void main() valid in a hosted environment, if so defined by the implementation. If I remember correctly, the argument concerned the placement of a semi-colon which made the phrase "or in some other implementation-defined manner" apply to the entire main function (and thus its return type) rather than only to its parameters.

    Nonetheless, it would be wise not to 'exploit' such a 'bug' in a standard.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    Dec 2006
    Location
    Washington
    Posts
    18
    Since I pre-date C, perhaps I can give some historical information that may or may not shed some light on this issue.

    Back in the days when memory was at a premium and a few kilobytes was a lot of system memory, the extra integer on the stack for the return type of main was a waste, particularly when the value was rarely checked. Void return types don't take stack space.

    In many embedded systems void main is still widely supported, particularly since it often occurrs that once the main is called, it is intended to never return. When you have a "resource constrained" system, spending an extra few bytes on a return type that never returns and consumes stack space that is never released, one can easily see why declaring main as void might be useful.

    In modern C and C++, we have processor caches of greater size than we had main system memory "a few decades ago." My first computer had 64K (total) of system memory. Today, the processor L1 cache is larger than that! Even embedded systems processors today have large instruction and data caches. I've written a lot of code on microcontrollers that have 32KB of on-chip SRAM. Declaring an int return type on the stack takes space that isn't needed, especially when the intention is that main never returns! When you have only a few hundred bytes of stack space, you think of such things. Others on this forum have suggested that they would use an int (actually a pair of ints!) data type when a char would do. Those of us who are definitively "old school" would never consider it!


    :davis:

  13. #13
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,485
    And if you believe that, you'll believe anything

    Which resource constrained system was C originally developed on - a PDP-11 with 32K of memory.

    What value did main return for the first 10 years of its life before it started to be ported to other platforms - that's right, an int.

    When did main get a void return - bingo, when lazy DOS programmers got a hold of it. With the crappy command line interpreter which could barely detect a failed program, few bothered to check the return result anyway. Thusly, void main was born.
    Lazy and ignorant programmers, book writers and complicit compilers do not make a standard.
    No other hosted platform outside of DOS (or its decendants) ever had this kind of slop as far as I know.

    > In many embedded systems void main is still widely supported,
    A free-standing implementation can do whatever it wants. It doesn't even have to have a main, or it can have many 'main-like' entry points.

    99% of newbies don't know the difference between "hosted" and "free-standing" so we just go with the "hosted" rules to save confusion.

    Great - another new year off to a start with a crummy "let's talk about void main" thread - sheesh!
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  14. #14
    Registered User
    Join Date
    Nov 2006
    Posts
    86
    yes im sorry i tried searching in this forum for other threads like this but i didnt realy find one quicky :P

  15. #15
    Registered User
    Join Date
    Jun 2005
    Posts
    6,252
    Quote Originally Posted by laserlight
    I cannot find the online reference to an article on this, but someone once pointed out that the C standard (C99, I think) contained a 'bug' (i.e., inaccurately phrased language) that unintentionally made void main() valid in a hosted environment, if so defined by the implementation. If I remember correctly, the argument concerned the placement of a semi-colon which made the phrase "or in some other implementation-defined manner" apply to the entire main function (and thus its return type) rather than only to its parameters.

    Nonetheless, it would be wise not to 'exploit' such a 'bug' in a standard.
    Technically, the standards do not prevent conforming implementations (whether freestanding or hosted) from supporting "void main()", or any other entry point into a program, as long as it supports "int main()".

    The catch is that one of the reasons for the standard is to define what a programmer can rely on from an implementation, and when moving their code between implementations (i.e. porting). void main() is not something a programmer can rely on. So, while a compliant implementation is allowed to support void main(), any code which employs that feature is non-compliant with the standards. A programmer who cares at all about portability of their code between standard compliant compilers cannot use it and also cannot legitimately claim that their programming complies with standard C or C++.

    The reason this topic comes up again and again is that there are quite a few text books and code examples which overtly claim, or more subtly imply, that code which employs "void main()" is compliant with requirements of the standards. Such claims or implications are false.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 08:36 PM
  2. Replies: 26
    Last Post: 11-30-2007, 02:51 AM
  3. Replies: 3
    Last Post: 05-13-2007, 08:55 AM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 04:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21