Thread: quick question about exit() and functions

  1. #1
    Linux is where it's at movl0x1's Avatar
    Join Date
    May 2007
    Posts
    72

    quick question about exit() and functions

    Hi. Just wondering. If I call a function that prints out a program's usage, such as,

    Code:
    void display_usage(void)
    {
      fprintf(stderr, "Usage: cvt [-dhb] number [-dhb]\n");
      exit(1);
    }
    Is there any difference in calling exit() from within the function, as opposed to
    returning to main() and then calling it from there? Such as,

    Code:
    int main(void)
    {
      display_usage();
      exit(1);
    
    }
    void display_usage(void)
    {
     fprintf(stderr, "Usage: cvt [-dhb] number [-dhb]\n");
    }
    thanks

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Not really, But the C89 standard specifies main must return a value.

    Getting a program's usage is strictly platform dependent - Do some research regarding your OS, if its Windows MSDN will help you out a lot.

  3. #3
    Linux is where it's at movl0x1's Avatar
    Join Date
    May 2007
    Posts
    72
    Windows? NO thanks. I program only on GNU/Linux. (<--- make Stallman happy!)

    thanks though And yes, I know that your supposed to return from main() I just left
    it out for this short example. I feel better returning to main() to exit() though, I don't
    know why.

    Oh wait a minute! Yeah, that means if I exit() in the function main() won't return. I think.
    I'll return to main() to exit().
    Last edited by movl0x1; 05-16-2007 at 05:42 AM.

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    In reality, when main() returns, it simply calls exit() with the return value. Technically, you can call exit() anywhere, but you should try to not spread calls to that function everywhere throughout your program. It will make the program harder to maintain, since there will be multiple ways for the program to exit() without coming back to main(). In addition, you generally want to cleanup a bit before terminating, so that's something to consider.

    All in all, just try to be neat about ending your program. The example you gave could work either way, but I would personally put the exit() inside main().

  5. #5
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    But he's breaking the standard (C89/90), He fails to return a value from main. While exit() is standard most compilers should still flag an error (when compiling in "strict" mode).

    You could do the following:

    Code:
    void MyExit(int code)
    {
        /* clean up vars here */
        exit(code);
    }
    
    void foo(void)
    {
        MyExit(0);
        return;
    }
    
    int main(void)
    {
        foo();
        return 0; /* never reached, just flag down the warnings */
    }
    But as MacGyver said, you should generally try and keep your exit()s grouped, as it can make your program hard to read (and workout where it ends). You can avoid it by designing your program well from the start, consider loops with boolean 'flags'.
    Last edited by zacs7; 05-16-2007 at 06:06 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 06-06-2008, 05:26 PM
  2. Odd memory leaks
    By VirtualAce in forum C++ Programming
    Replies: 11
    Last Post: 05-25-2006, 12:56 AM
  3. Replies: 1
    Last Post: 03-21-2006, 07:52 AM
  4. Couple of Q's.....
    By oobootsy1 in forum C++ Programming
    Replies: 18
    Last Post: 02-23-2004, 02:03 PM
  5. what does this warningmean???
    By kreyes in forum C Programming
    Replies: 5
    Last Post: 03-04-2002, 07:53 AM