GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
 
 
Thread Tools Search this Thread Rating: Thread Rating: 3 votes, 4.33 average.
  #1  
Old 04-Apr-2007, 17:39
IndioDoido IndioDoido is offline
New Member
 
Join Date: Apr 2007
Posts: 4
IndioDoido is on a distinguished road

How to use exit in C


Hi, I'm doing a simple program in C, and I need a option that terminates the program. For example, instead of appearing "Press any key to continue..." the program will instantly close. I've been searching and I found some references on a exit command, but I don't have any idea on how to use it :-S

Can anyone help me?
  #2  
Old 04-Apr-2007, 19:19
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 6,153
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: How to use exit in C


Don't put system("pause"); in the program, since that is what causes the message to appear and causes it to wait for a keypress.

A "normal" program termination is a return from main()

CPP / C++ / C Code:

#include <stdio.h>
int main()
{
    printf("Goodbye, world!\n");
    return 0;
}

If you are way down in some nested calls of functions and you want to "exit" immediately, without returning all of the way to main(), then use exit:

CPP / C++ / C Code:
#include <stdio.h>
.
.
.
.
void some_function()
{
.
.
.
    if (something_bad_happened) {
        exit(EXIT_FAILURE);
    }
    else {
        proceed_merrily_on_your_way();
    }
}
.
.
.


Look up standard library functions at places like this: Standard C Library. Click on <stdlib.h> and scroll down to exit

Regards,

Dave
  #3  
Old 05-Apr-2007, 06:12
IndioDoido IndioDoido is offline
New Member
 
Join Date: Apr 2007
Posts: 4
IndioDoido is on a distinguished road

Re: How to use exit in C


Hi Dave, thanks for the reply

Ok, now i understand how to exit a program without the "Press any key to continue...".

What is the best option to exit a program, when you are using files and lot's of functions?
Is it the exit(EXIT_SUCCESS); or exit(EXIT_FAILURE); ?
  #4  
Old 05-Apr-2007, 07:53
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 6,153
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: How to use exit in C


The argument to the "exit()" function is passed back to the calling process by the operating system. If you run the program from a command line, a command line script may be able use the return value to cause it to do something, or it may simply ignore the return value. If the calling program is another program (through a system() call, for example), the return value may mean something significant to allow the calling program take action dependent on the returned value.

The actual mechanism for returning a value is not part of the C language (it's operating-system dependent). The only two values defined in Standard C are the EXIT_SUCCESS, or EXIT_FAILURE macros in <stdlib.h>. That is, "exit(3)", for example, may cause different actions, depending on the operating system.

The statement "exit(EXIT_SUCCESS)" is equivalent to exit(0). The result of an "exit(0)" is the same as a "return 0;" at the end of main().

Typically people use "return(0)" or "exit(0)" or "exit(EXIT_SUCCESS)" unless they want to give the calling program to discriminate between a satisfactory termination and termination caused by an error condition.

Regards,

Dave
  #5  
Old 06-Apr-2007, 17:21
IndioDoido IndioDoido is offline
New Member
 
Join Date: Apr 2007
Posts: 4
IndioDoido is on a distinguished road

Re: How to use exit in C


Hey!

Once again, thanks for the reply.
It was clear as water

Regards,
IndioDoido
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Please Help Me To Build My Calendar!!! suriacute85 Java Forum 0 05-Oct-2006 19:39
functions seems not to exit from a certain code block jaro C Programming Language 3 21-Mar-2006 23:08
When to use exit() Instead of return? BobbyMurcerFan C Programming Language 10 24-Nov-2004 23:44
child pid xxx exit signal Segmentation fault (11) bezak Apache Web Server Forum 1 24-Nov-2003 09:18

Network Sites: GIDNetwork · GIDApp · GIDBlog · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 05:34.


vBulletin, Copyright © 2000 - 2014, Jelsoft Enterprises Ltd.