开发者

Void in main and keeping Dos program from dying [duplicate]

This question already has answers here: Closed 12 years ago.

Poss开发者_StackOverflow中文版ible Duplicate:

Difference between void main and int main?

Alright, so I'm using bloodshed complier, first thing I should note is I'm using a 2001 edition C++ for the absolute beginner, was there any changes to C++ since 2001 that would effect the validity of this book? I ask because I know php has php5 and now php6 but I haven't seen any C++03.

Now for the reason I asked that, in this code it uses,

void main (void)

why would I want an argument to be void? what does this do.

But when I run the void main (void) in my compiler it says that main must have a "int" before it. So I can't have "void" main (void);

Also, once the code runs through, as long as there isn't a "cin" the program closes. Is this normal behavior, is there a better way to stop this besides making a cin at the end of my program?


Burn that book. Not only is void main not conformant to any C++ standard (modern or old), but declaring a parameter-less function with (void) instead of () is a C idiom that is discouraged in C++. From this sample, I shudder to think what other horrors that book might contain.

The issue with the console window closing is completely unrelated to the contents of your program. Windows has a behavior where if you launch a console program directly, it will automatically close the console window when the program ends. To avoid this, open a console window yourself, and run your program from within it. The console window will stay open as long as you want it to. (And please refrain from using the unfortunately popular practice of adding an extra dummy input or a "pause" call at the end of your program to keep the window open. You should not be adding code to your program to compensate for a particular way that you happen to be launching it; just launch it the right way and leave your poor code alone.)


What there have been since 2001 are changes to C++ compilers, which on the whole are much pickier about how standards-conformant your C++ must be before they accept it. For example, gcc has gone from 2.95 or so all the way to 4.4.2. Earlier compilers may have let you slide with this form of main, as they would several old C-isms that are not proper C++, but it's not the language that's changed in this case (at least, as far as the standard goes).

Having said that: yes, C++ has changed, and is changing, though not quite yet at the official standards level. Different compilers support those changes to different extents. I don't think you're likely to be impacted by them, but you may want to be aware of them at any rate. See:

  • C++ TR1
  • C++0x, the forthcoming standard

As far as how to end your program. I recommend a simple "return 0;" at the point in main where you want to exit – or use an exit code other than 0 if you want to exit with an error. You can omit the return, though, and many examples do this, though I personally think it's bad style.

I understand you're using cin to pause the application before it exits. This isn't required for C++ apps in general, it's just a convenience for running the app in the manner you are. It's OK for examples, but you'd definitely want to avoid that for a real application.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜