is there any command to end a program.
Printable View
is there any command to end a program.
I believe there is a function, exit() ... you must include cstlib.h .... do a google search on that :) Good luck!
Yep, std::exit( int ) in <cstdlib> .
Isn't it also in stdlib.h?
anyway, isn't it easier just to return 0? :confused:
As I understand it, C standard headers (including stdlib.h) are best used in C++ by dropping the ".h" and putting a "c" in front of it. This also puts the functions into the namespace "std". Although your way works, it's deprecated.Quote:
Originally posted by Nuke
Isn't it also in stdlib.h?
You can, but only if you're in main. What if you call another function and decide (for some reason) to exit from there?Quote:
anyway, isn't it easier just to return 0? :confused:
>Isn't it also in stdlib.h?
stdlib.h or cstdlib. However, take note that when you use cstdlib, everything will be in the std namespace.
>anyway, isn't it easier just to return 0?
From the control of main, yes. From elsewhere it's not so easy to return in one fell swoop without lots of annoying flags (exit is easier then). Also, for some compilers, exit is the only way to be sure that the return code you pass is not discarded. Many compilers will simply return 0 even if your code specifies a different return code.
Really? What compilers do this, and why (it's so easy not to do it).Quote:
Many compilers will simply return 0 even if your code specifies a different return code.
>What compilers do this
Pretty much any compiler that allows void main probably does so. The standard requires that main be invoked as if called like this:
However, some compilers (mostly older ones) do it more like this:Code:exit(main(argc, argv);
So if you have to have a useful exit code, it's more portable (in reality) to use the exit function instead of the return keyword.Code:main(argc, argv);
exit(0);
I know for sure that VC++ does not do it, although it allows void main.
Don't know about exotic compilers though, don't really care for them.
hey,
i just joined the team!
how abt "return;"??? - for void main()
return 0; - for int main()
wont that work too???
You shouldn't use void main( ) in the first place. It is nowhere in the standard for C++, and I'm pretty sure its not allowed in C either.
void main() is the devil.
anyways, i dont see how using exit() is more portable than return 0 is. are you saying that if you tried to check the return value of a program, it will be 0, so it could cause some errors if you were doing something thats dependent on what a program returned? pretty complex concept or its just me :).
The main time you want to use exit( ) is if you want to quit the program from a function other than main( ). In this case, a simple return statement won't do, so you have to use the exit( ) command.