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?
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?
What's it's writing date (or copyright)?
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 $$$$!
Is void main considered bad practice because of portability issues, or is there another reason?
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?
Oh no, not them. *Shivers*...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.
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:
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 mainret; // ... blah blah blah (lots of code) blah blah blah ... mainret = main(argc, argv, envp);
Code:int main(void); int main(int argc); int main(int argc, char *argv[]); int main(int argc, char *argv[], char *envp[]);