Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C (http://www.programmingforums.org/forum60.html)
-   -   Calling exit(???) functions (http://www.programmingforums.org/showthread.php?t=14890)

BstrucT Jan 8th, 2008 4:40 AM

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.

Jimbo Jan 8th, 2008 5:02 AM

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.

jim mcnamara Jan 8th, 2008 5:27 PM

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.

BstrucT Jan 9th, 2008 12:56 AM

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

lectricpharaoh Jan 9th, 2008 2:02 AM

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.

BstrucT Jan 9th, 2008 4:41 AM

Re: Calling exit(???) functions
 
Quote:

Originally Posted by lectricpharaoh (Post 139383)
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.

WaltP Jan 9th, 2008 4:13 PM

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?

Druid Jan 9th, 2008 4:19 PM

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.

BstrucT Jan 10th, 2008 12:50 AM

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;
}


grumpy Jan 10th, 2008 4:42 AM

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.


All times are GMT -5. The time now is 3:43 AM.

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