any command to end a program in C?

If you have any questions on programming, this is the place to ask them, whether you're a newbie or an experienced programmer. Discussion on programming in general is also welcome. We will help you with programming homework, but we will not do your work for you! Any porting requests must be made in Developmental Ideas.
Post Reply
spiroth10
Mental DCEmu
Mental DCEmu
Posts: 304
https://www.artistsworkshop.eu/meble-kuchenne-na-wymiar-warszawa-gdzie-zamowic/
Joined: Mon Sep 01, 2003 4:57 pm
Has thanked: 0
Been thanked: 0

any command to end a program in C?

Post by spiroth10 »

Ok, I come to this problem a lot, and I don't know how I can actually efficiently program in C without this knowledge: is there a command to halt the program and end it right where it is? The program I just recoded uses a lot of loops that I can't just use break; with. and they're embedded in if statements to make them even harder to stop. the only way to do this would be to use an exit function of some sort, like basic's exit command... It's driving me pretty nutty, and I cant even find a command like this in C the Complete reference third edition (and that's a pretty extensive book).
Sanchez
DCEmu Ex-Admin
DCEmu Ex-Admin
Posts: 1098
Joined: Wed Oct 17, 2001 7:44 pm
Has thanked: 0
Been thanked: 0

Post by Sanchez »

There's an exit function exit(int) but it's not really the right way to do it...
"This is worse than when the Raccoon got in the copier!"
spiroth10
Mental DCEmu
Mental DCEmu
Posts: 304
Joined: Mon Sep 01, 2003 4:57 pm
Has thanked: 0
Been thanked: 0

Post by spiroth10 »

thanks Sanchez. I relly dont care if its the right way to do it, as long as it doesnt let you continue the game (thats what I'm making) after you die... it doesnt registers that your characters dead (i mean the functions that say your dead works) but you can keep playing if you dont mind the "your dead" message... :p thanks a lot.
Image
Rev. Layle
Insane DCEmu
Insane DCEmu
Posts: 190
Joined: Sun Jun 27, 2004 8:35 pm
Location: stillwater, ok
Has thanked: 0
Been thanked: 0
Contact:

Post by Rev. Layle »

Sanchez wrote:There's an exit function exit(int) but it's not really the right way to do it...
well, isn't that what exit() is for? ;)

well it does the trick fine on linux and DOS. other OS'es maybe have more graceful exit functions created just for that OS
Sanchez
DCEmu Ex-Admin
DCEmu Ex-Admin
Posts: 1098
Joined: Wed Oct 17, 2001 7:44 pm
Has thanked: 0
Been thanked: 0

Post by Sanchez »

What I mean is exiting like he's doing in the middle of a loop is not clean, especially if resources have been malloc'd, etc...
"This is worse than when the Raccoon got in the copier!"
ninja
DCEmu's own ninja
DCEmu's own ninja
Posts: 2381
Joined: Fri Sep 27, 2002 12:48 am
Has thanked: 0
Been thanked: 0

Post by ninja »

why not do something along the lines of an if then statement during the loop, that way when the value is true (dead) you could send it off to do whatever you please.
Delete my posting account.

Thanks.
Rev. Layle
Insane DCEmu
Insane DCEmu
Posts: 190
Joined: Sun Jun 27, 2004 8:35 pm
Location: stillwater, ok
Has thanked: 0
Been thanked: 0
Contact:

Post by Rev. Layle »

Sanchez wrote:What I mean is exiting like he's doing in the middle of a loop is not clean, especially if resources have been malloc'd, etc...
oh yeah, duh. one should write a custome program ending function then call exit. also, some OS'es will automatically cleanup any allocated memory for the program when terminated (like Win32, most DOS apps, Mac OS, etc.... dunno how it works in the console world, i assume exit() would not ever be a thing to call anyways
User avatar
SinisterTengu
DC Developer
DC Developer
Posts: 382
Joined: Wed Oct 17, 2001 7:44 pm
Location: Arlington, WA
Has thanked: 0
Been thanked: 0

Post by SinisterTengu »

you could also just make your loops dependant on that the character is not dead..

Code: Select all

while(dead == false) {
    ...
}
Then when your character does die, it will exit whatever loops are dependant, and you will get past those loops to where you can clean up and exit nicely.
Image
Image
User avatar
toastman
Iron Fist of Justice
Iron Fist of Justice
Posts: 4933
Joined: Sat Nov 10, 2001 3:08 am
Location: New Orleans
Has thanked: 0
Been thanked: 0
Contact:

Post by toastman »

Rev. Layle wrote:
Sanchez wrote:What I mean is exiting like he's doing in the middle of a loop is not clean, especially if resources have been malloc'd, etc...
oh yeah, duh. one should write a custome program ending function then call exit. also, some OS'es will automatically cleanup any allocated memory for the program when terminated (like Win32, most DOS apps, Mac OS, etc.... dunno how it works in the console world, i assume exit() would not ever be a thing to call anyways
Never depend on system behavior to do something you should take care of yourself.
No signature.
Post Reply