ending a program

This is a discussion on ending a program within the C++ Programming forums, part of the General Programming Boards category; is there any command to end a program....

  1. #1
    Registered User
    Join Date
    May 2003
    Posts
    4

    ending a program

    is there any command to end a program.

  2. #2
    plzduntlakliekthiskthx
    Join Date
    Oct 2002
    Posts
    138
    I believe there is a function, exit() ... you must include cstlib.h .... do a google search on that Good luck!

  3. #3
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Yep, std::exit( int ) in <cstdlib> .
    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.

  4. #4
    Registered User
    Join Date
    Mar 2003
    Posts
    73
    Isn't it also in stdlib.h?

    anyway, isn't it easier just to return 0?

  5. #5
    Registered User
    Join Date
    Dec 2002
    Posts
    56
    Originally posted by Nuke
    Isn't it also in stdlib.h?
    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.

    anyway, isn't it easier just to return 0?
    You can, but only if you're in main. What if you call another function and decide (for some reason) to exit from there?
    Last edited by roktsyntst; 06-01-2003 at 09:56 PM.

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >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.
    My best code is written with the delete key.

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    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).
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >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:
    Code:
    exit(main(argc, argv);
    However, some compilers (mostly older ones) do it more like this:
    Code:
    main(argc, argv);
    exit(0);
    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.
    My best code is written with the delete key.

  9. #9
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    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.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  10. #10
    Registered User
    Join Date
    Jun 2003
    Posts
    11
    hey,

    i just joined the team!

    how abt "return;"??? - for void main()
    return 0; - for int main()

    wont that work too???

  11. #11
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    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.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  12. #12
    Registered User
    Join Date
    Sep 2001
    Posts
    305
    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 .

  13. #13
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    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.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 10:17 PM
  2. Can someome help me with a program please?
    By WinterInChicago in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2006, 10:58 PM
  3. I need some help with my program please.
    By agentxx04 in forum C Programming
    Replies: 9
    Last Post: 09-26-2004, 07:51 AM
  4. help with ending program
    By Derek in forum C Programming
    Replies: 13
    Last Post: 06-25-2003, 12:10 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

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