Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jan 8th, 2008, 3:40 AM   #1
BstrucT
Hobbyist Programmer
 
BstrucT's Avatar
 
Join Date: Dec 2007
Location: Durban, South-Africa
Posts: 176
Rep Power: 1 BstrucT is on a distinguished road
Calling exit(???) functions

Hi 2 All

I have a slight problem with the exit() functions used in C.

I would like to know what the difference is between exit(0), exit(1), and exit(-1).

These are some of the exit functions I have encountered so far.

All input will be appreciated.
__________________
The more the human race tries to change everything, when not needed, the less will they be able to change themselves when they need to.
BstrucT is online now   Reply With Quote
Old Jan 8th, 2008, 4:02 AM   #2
Jimbo
Battle Programmer
 
Jimbo's Avatar
 
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 754
Rep Power: 3 Jimbo is on a distinguished road
Re: Calling exit(???) functions

They're the same function, just passed a different value as the parameter. They'll cause your program to terminate, and it will return the passed value back to the operating system. Convention dictates that a return value of 0 means that all went well and anything else is an error code.
__________________
<insert disclaimer here>
<insert shameless plug for Visual Studio here>
Jimbo is offline   Reply With Quote
Old Jan 8th, 2008, 4:27 PM   #3
jim mcnamara
Hobbyist Programmer
 
Join Date: Jun 2005
Location: New Mexico
Posts: 228
Rep Power: 4 jim mcnamara is on a distinguished road
Re: Calling exit(???) functions

exit(int status) is defined by standard to close open files, stop the executable image from running and to return the value of status to the calling program. In MS this could be windows explorer, a DOS sub-shell, or parent program. In unix it could be a parent program or the shell. EXIT_SUCCESS and EXIT_FAILURE are macros that are defined in the standard.

What you put in a "special" status depends on what the "receiver" of the status wants or can deal with, so it is implmentation dependent. Read the manual for your compiler. As Jimbo mentioned there are some conventions. There are also some "proposed" exit values that you will see from time to time - an example is sysexits.h which defines macros like EX_USER.

This is why you will see different error returns from different programs and they may have special meaning. You can google for sysexits.h and look at the include file if you want.
jim mcnamara is offline   Reply With Quote
Old Jan 8th, 2008, 11:56 PM   #4
BstrucT
Hobbyist Programmer
 
BstrucT's Avatar
 
Join Date: Dec 2007
Location: Durban, South-Africa
Posts: 176
Rep Power: 1 BstrucT is on a distinguished road
Re: Calling exit(???) functions

Thanks

It makes alot more sense now.

Just wondering...

Why does the C BOOK use EXIT_SUCCES and not return(0)?
All input much appreciated.

Thanks
BstrucT
__________________
The more the human race tries to change everything, when not needed, the less will they be able to change themselves when they need to.
BstrucT is online now   Reply With Quote
Old Jan 9th, 2008, 1:02 AM   #5
lectricpharaoh
Caffeinated Neural Net
 
lectricpharaoh's Avatar
 
Join Date: Jun 2005
Location: Dry west coast of Canada
Posts: 1,010
Rep Power: 5 lectricpharaoh will become famous soon enough
Re: Calling exit(???) functions

Using EXIT_SUCCESS makes it clear what the intended result is. The principle here is that named constants are clearer than numbers in most cases.
__________________
And once again, Probability proves itself willing to sneak into a back alley and service Drama as would a copper-piece harlot.
- Vaarsuvius, Order of the Stick
lectricpharaoh is offline   Reply With Quote
Old Jan 9th, 2008, 3:41 AM   #6
BstrucT
Hobbyist Programmer
 
BstrucT's Avatar
 
Join Date: Dec 2007
Location: Durban, South-Africa
Posts: 176
Rep Power: 1 BstrucT is on a distinguished road
Re: Calling exit(???) functions

Quote:
Originally Posted by lectricpharaoh View Post
named constants are clearer than numbers in most cases.
Thanks man, I have just finished re- doing my little calculator application with the advise all has given. Will post shortly. Also implemented the exit(EXIT_SUCCESS); as I can now make out easier where it is, by examining the source code, especially since I used switch() statements in my little calculator app.

I definitely see the point in using it.

Thanks again.
__________________
The more the human race tries to change everything, when not needed, the less will they be able to change themselves when they need to.
BstrucT is online now   Reply With Quote
Old Jan 9th, 2008, 3:13 PM   #7
WaltP
Programmer
 
Join Date: Oct 2007
Posts: 39
Rep Power: 0 WaltP is on a distinguished road
Re: Calling exit(???) functions

I don't understand why you are using exit(). Why call a function when you can simply return to leave the program?
__________________
Testing 001 010 011 100....
WaltP is offline   Reply With Quote
Old Jan 9th, 2008, 3:19 PM   #8
Druid
Programmer
 
Join Date: Mar 2006
Posts: 40
Rep Power: 0 Druid is on a distinguished road
Re: Calling exit(???) functions

exit() is useful in error handling... if you don't want the function to continue working due to some error and you want to stop the execution of the application, exit is appropriate.
Druid is offline   Reply With Quote
Old Jan 9th, 2008, 11:50 PM   #9
BstrucT
Hobbyist Programmer
 
BstrucT's Avatar
 
Join Date: Dec 2007
Location: Durban, South-Africa
Posts: 176
Rep Power: 1 BstrucT is on a distinguished road
Re: Calling exit(???) functions

Hi Guys

I used the exit(EXIT_SUCCESS) when I want the program to exit completely and stop execution.

I wrote a little calculator app(very basic) that uses a menu system I implemented by means of switch(). I used return() to get the user back to the main menu, and used exit(EXIT_SUCCESS) to exit the program completely.

I have attached the source code because I would like to know if this is the correct usage of these functions. This is basically the same program from my previous post "BEGINNER C CALCULATOR PROBLEM", only difference I took the advise I got from the experts here, and have revised it.

/* C - Calculator Revised.  */
/* Bstruct -> 09/01/2008 .  */

#include <stdio.h>
#include <stdlib.h>

/*******Math Functions*******/
float addition(float a,float b);
float (*a)(float a,float b);           /* Pointer Declaration */

float subtraction(float a,float b);
float (*s)(float a,float b);           /* Pointer Declaration */

float multiplication(float a,float b);
float (*m)(float a,float b);           /* Pointer Declaration */

float division(float a,float b);
float (*d)(float a,float b);           /* Pointer Declaration */

int user_menu(void);

/*Variables to hold user - input*/
float nr_1,nr_2;

main()
{
      puts("\t\t##############");
      puts("\t\t###[C-CALC]###");
      puts("\t\t##############");
      printf("\n\n\n");
      
      a = addition;                 /* Initializing Pointer to Function */
      s = subtraction;              /* Initializing Pointer to Function */
      m = multiplication;           /* Initializing Pointer to Function */
      d = division;                 /* Initializing Pointer to Function */
      
      /* Now to get the input from user*/
      
      printf("Enter any two real values to be calculated...\n");
      printf("Hit [RETURN] when done:\n\n\n");
      
      printf("1st Number:\n");
      scanf("%f", &nr_1);
      
      /* Check that user didn't enter a NULL */
      if (nr_1 == 0)
      {
               printf("\n\aYou can't enter a NULL, program will re-start...\n");
               return (main());
               }
               
      printf("2nd Number:\n");
      scanf("%f", &nr_2);
      
      /* Check that user didn't enter a NULL */
      if (nr_2 == 0)
      {
               printf("\n\aYou can't enter a NULL, program will re-start...\n");
               return (main());
               }
while(1)
{
        puts("\n\n\nChoose your calculation method...");
        
switch(user_menu())
{
case 1:
{
     printf("\n%.2f + %.2f = %.2f", nr_1,nr_2,a(nr_1,nr_2));
     break;
}
case 2:
{
     if (nr_1 > nr_2)
     {
              printf("\n%.2f - %.2f = %.2f", nr_1,nr_2,s(nr_1,nr_2));
              }
     if (nr_2 > nr_1)
     {
              printf("\n%.2f - %.2f = %.2f", nr_2,nr_1,s(nr_1,nr_2));
              }
              break;
}
case 3:
{
              printf("\n%.2f X %.2f = %.2f", nr_1,nr_2,m(nr_1,nr_2));
              
              break;
}     
case 4:
{
     if (nr_1 > nr_2)
     {
              printf("\n%.2f / %.2f = %.3f", nr_1,nr_2,d(nr_1,nr_2));
              }
     if (nr_2 > nr_1)
     {
              printf("\n%.2f / %.2f = %.3f", nr_2,nr_1,d(nr_1,nr_2));
              }
              break;
}
default:
        {
              printf("\n\nIncorrect Choice, Please Re-Enter...\n");
              }          
case 5:
{
     printf("\n\a\n\nReturning to main menu...\n");
     return(main());
     }
case 6:
{
     printf("\n\nExiting now...\n");
     exit(EXIT_SUCCESS);
     }  

}      /*Closing Switch*/
}      /*Closing While */
}      /*Closing Main()*/  
        
/*****Addition Function******/
float addition(float nr_1,float nr_2)
{
     return (nr_1 + nr_2);
}
/*****Subtraction Function***/ 
float subtraction(float nr_1,float nr_2)
{
      if (nr_1 > nr_2)
      {
            return (nr_1 - nr_2);
            }
            else
            return (nr_2 - nr_1);
}
/***Multiplication Function**/
float multiplication(float nr_1,float nr_2)
{
      return ((nr_1)*(nr_2));
}
/*****Division Function******/
float division(float nr_1,float nr_2)
{
      if (nr_1 > nr_2)
      {
            return (nr_1 / nr_2);
            }
            else
            return (nr_2 / nr_1);
}      
/*********User Menu**********/
int user_menu(void)
{
    int selection_1;
    
    puts("1         -         Addition");
    puts("2         -         Subtraction");
    puts("3         -         Multiplication");
    puts("4         -         Division");
    printf("\n");
    puts("5        -          Re - Start");
    puts("6        -          Exit");
    
    scanf("%d", &selection_1);
    
    return selection_1;
}
__________________
The more the human race tries to change everything, when not needed, the less will they be able to change themselves when they need to.
BstrucT is online now   Reply With Quote
Old Jan 10th, 2008, 3:42 AM   #10
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,209
Rep Power: 5 grumpy is on a distinguished road
Re: Calling exit(???) functions

It is often considered poor style to call main() recursively, and it is actually illegal in C++. Technically, it is a VERY bad idea to do that in response to input (from either a user or a file) as the program has no way of controlling the depth of recursion, and deep levels of recursion can consume resources (eg stack space) available to the program. Users of your program will normally expect it can happily loop forever if they enter appropriate input; they will not expect it to consume system resources (eg memory) or to - eventually - terminate abnormally when it consumes all available resources.

Better to set up looping constructs within main() so you can better understand what the program does, rather than calling main() recursively everywhere. A side effect of that is that "return status;" and "exit(status);" will have the same effect (at least, within main()).

You are referring to zero values as NULLs in prompts to the user. Not technically incorrect, but those words only makes sense to the user of your program if that user is a C/C++ programmer. If you want to test if the user has hit <RETURN> checking a field read in by scanf() is not the way to do it, because that function will wait for non-space input.

Your division() and subtraction() functions will produce results that will surprise the user of your program on occasion. Users tend to complain when programs behave in surprising ways.

I fail to see the point of using function pointers in your code, but there's no real harm I guess.
grumpy is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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

BB 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
Opposite of global in functions davil PHP 7 Dec 19th, 2007 4:40 AM
Calling .net functions from C++ uman C++ 2 Jul 26th, 2006 4:36 PM
binding between ordinary member functions & polymorphic member functions ASH C++ 2 May 11th, 2006 4:36 AM
Exporting Functions victorsk Visual Basic 1 May 18th, 2005 2:32 PM
User-defined creatNode and deleteNode functions for a doubly-linked list jgs C 2 Apr 28th, 2005 8:53 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 10:57 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC