Thread: void main()

  1. #1
    ___
    Join Date
    Jun 2003
    Posts
    806

    void main()

    Alright. Well I know that void main() is improper and all. I have learned that much from you guys at least. My question is why do all of the books I pick up and go through use void main() and not int main() with a return of 0? It's sort of annoying to have to go and then modify every bit of code they give you...
    "When I die I want to pass peacefully in my sleep like my grandfather did, not screaming and yelling like the passengers in his car."

  2. #2
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Maybe you're just picking up bad books.

    There are lots of people who can program C++. Many of them don't do it correctly, even very intelligent people often don't know every nuance.

    Pick up books by the big name people who are the real experts (e.g. Josuttis, Sutter, Meyers, et al) which were written after C++98, and you should be OK.

  3. #3
    Registered User Casey's Avatar
    Join Date
    Jun 2003
    Posts
    47
    >>why do all of the books I pick up and go through use void main()
    It's simple, the author doesn't know the language he's writing about. If he did, he would know that void main isn't legal in standard C++.

  4. #4
    ___
    Join Date
    Jun 2003
    Posts
    806
    Ok. The book I'm using now (Beginning VC++ 6 by Ivor Horton) and he teaches properly, but I have a couple other books in my shelf that teach using void main().
    "When I die I want to pass peacefully in my sleep like my grandfather did, not screaming and yelling like the passengers in his car."

  5. #5
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    I hope Salem doesnt see this...

    The void main topic is a very common one and is answered hundreds of times within this very forum, as a quick search will prove. Its also found in the FAQ and tutorials, both of which have links in my signature.

  6. #6
    ___
    Join Date
    Jun 2003
    Posts
    806
    I don't use it. Just allot of books do.
    "When I die I want to pass peacefully in my sleep like my grandfather did, not screaming and yelling like the passengers in his car."

  7. #7
    Registered User codingmaster's Avatar
    Join Date
    Sep 2002
    Posts
    309
    I think, that the void main() discussion is the oldest discussion at this board.....

    I don't know why the new users have to open threads, instead of searching the board...... that's trash....

    And the void main() discussion is so old...... and boring.....

    I hope, that Salem doesn't get a heart attack, when he sees another void main() discussion

    Poor, poor Salem

    Instead of start trashing this board with older discussions, please use the search function.....

    just search for: void main, I got 4792 topics.....

  8. #8
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Are people even reading the first post? It's not a discussion about which is correct, he asks why authors of books use the wrong form.

    I wouldn't read anything in a book from an author which can't even get int main() correct; who knows what else the author will do?

  9. #9
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    My guess is either a) the authors are not up to date on C++, or b) they save printing space by saving a line each time they have a main, and write hideous code along the lines of this:

    Code:
    void main() {
       int x = 3;
       f(x); }
    Either way, it doesn't discount the book right off as being bad, but I'd be wary of it.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  10. #10
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    >>Are people even reading the first post? It's not a discussion about which is correct, he asks why authors of books use the wrong form.

    Which has been covered.

  11. #11
    Registered User
    Join Date
    Nov 2002
    Posts
    126
    Either way, it doesn't discount the book right off as being bad, but I'd be wary of it.
    Well, chances are that if the book contains void main(), it was probably written over 6 years ago. So it will probably include lots of other non-standard code. The problem is that it's not 1996 anymore. The language has changed quite a bit in 5 years. I find it annoying that teachers and professors, for whatever reason, continue to use pre-standard books to teach the language. If the book was written after '98 and still contains dinosaurs like void main and headers ending in .h, then the author needs to wake up and smell the 21st century. It is my opinion that we as programmers have a responsibility to keep up with the language standards if we want to get the most out of it. Standard C++ is a much better language than pre-1998 C++.

  12. #12
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    viod main() was still illegal pre-1998. void has never once been a valid return type from main(), in C or in C++. Only in Java is that a legal return type for main().

    Technically, there is a loophole in which void main() can be legal C; the people who implement a compiler are technically allowed, under ANSI-C, to permit alternate definitions of main(), including allowing void or any other return type. However, this is not a portable solution, it merely says that implementations may extend the standard to permit other forms of main().

    viod main() is not permitted under C++, and there are no loopholes here. In fact, the ANSI-C++ standard states that void main() must produce at the very least, a warning during compilation.
    Last edited by Cat; 07-11-2003 at 09:25 PM.

  13. #13

  14. #14
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916

    couldn't resist

    I don't think there are any compilers that will compile your program if you have any functions with a viod return type But...yeah. void main() is bad, don't use it, or Salem will beat you with a stick. If that isn't enough incentive to use int main(), I don't know what is.
    Away.

  15. #15
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    #define viod void
    !!!!!
    !!!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 03-05-2009, 10:25 AM
  2. can someone help me with these errors please code included
    By geekrockergal in forum C Programming
    Replies: 7
    Last Post: 02-10-2009, 02:20 PM
  3. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  4. need help with handelling multiple source files
    By DarkMortar in forum C++ Programming
    Replies: 38
    Last Post: 05-26-2006, 10:46 PM
  5. getting a headache
    By sreetvert83 in forum C++ Programming
    Replies: 41
    Last Post: 09-30-2005, 05:20 AM