My book may be outdated...

This is a discussion on My book may be outdated... within the C++ Programming forums, part of the General Programming Boards category; My book (Starting Out With C++ (3rd Edition) uses void main (void) in it's programming examples. It also uses <iostream.h> ...

  1. #1
    Un Artiste Extraordinaire volk's Avatar
    Join Date
    Dec 2002
    Posts
    357

    My book may be outdated...

    My book (Starting Out With C++ (3rd Edition) uses void main (void) in it's programming examples.

    It also uses <iostream.h> instead of <iostream>

    Comments?

  2. #2
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    What's it's writing date (or copyright)?

  3. #3
    Un Artiste Extraordinaire volk's Avatar
    Join Date
    Dec 2002
    Posts
    357
    It says 2001.

  4. #4
    Banned
    Join Date
    Jan 2003
    Posts
    1,708
    it's out of date because it uses iostream.h, not because it uses void main. The latest standard has its header files without the '.h' at the end, and it would use the std:: namespace. void main is just bad practice; you can have bad practice and the latest standard (I know, shocked me too!)

    EDIT: although to tell you the truth for starters using void main is good practice; the programs are always extremely small, and the reader may get confused because he/she probably won't know what the return means. It seems best to start off with the simplest programs possible, and then include the nifty $$$$!

  5. #5
    Un Artiste Extraordinaire volk's Avatar
    Join Date
    Dec 2002
    Posts
    357
    Is void main considered bad practice because of portability issues, or is there another reason?

  6. #6
    Banned
    Join Date
    Jan 2003
    Posts
    1,708
    it's not always such a bad thing,and to tell you the truth the only reason a lot of people say its a bad thing is because bigger/beefier programmers say its a bad thing. But if main returns an integer, then you know for an absolute fact that program execution got to the end of the function. What if for some reason your program goes into an endless loop. If your main doesn't return a value, how do you know what it's doing?

  7. #7
    Un Artiste Extraordinaire volk's Avatar
    Join Date
    Dec 2002
    Posts
    357
    ...to tell you the truth the only reason a lot of people say its a bad thing is because bigger/beefier programmers say its a bad thing.
    Oh no, not them. *Shivers*

  8. #8
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    Why do you use void main()? Because the standard says so. For all you non-ASM programmers, any integer value is returned from a function in the register eax (in x86). So, in a void main(), that eax could be anything in the world, probably something left over from an operation. If you specify int main(), you can in C/C++ set the return value in eax. That way, you're not sending back to the runtime some rubbish number.

    This line of code I found in the MS runtime for C/C++. If you take a look those sources you'd find many interesting things, really! Anyways, there are a lot of these "versions of this line of code." Here's just one of them; it implies that main return something:
    Code:
    int mainret;
    // ... blah blah blah (lots of code) blah blah blah ...
    mainret = main(argc, argv, envp);
    And btw, the third parameter is part of the standard. It shows some environmental strings and stuff specified by the system. In fact, you can write your mains() like this and they are legal:
    Code:
    int main(void);
    int main(int argc);
    int main(int argc, char *argv[]);
    int main(int argc, char *argv[], char *envp[]);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. URGENT: Help wanted...in C
    By iamjimjohn in forum C Programming
    Replies: 16
    Last Post: 05-18-2007, 05:46 AM
  2. Books on C and C++
    By kermi3 in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-28-2002, 03:18 PM
  3. Newbie - MFC code from a book in VC++.Net
    By Guardian in forum Windows Programming
    Replies: 2
    Last Post: 04-27-2002, 07:17 PM
  4. C++: Reference Book, GUI, Networking & Beyond
    By kuphryn in forum C++ Programming
    Replies: 4
    Last Post: 11-10-2001, 07:03 PM

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