Thread: return or exit

  1. #1
    Registered User ch4's Avatar
    Join Date
    Jan 2007
    Posts
    154

    return or exit

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
     float light[3][3];
     FILE *Input;
    
    
         Input = fopen("bunny2.mtl","rb");
         if(Input==NULL)
         {
          fprintf(stderr,"Cannot find bunny2.mtl file.\n");
          exit(-1);   //Exit
          return -1; //Exit
         }
         
         fclose(Input);
    
     return 0;
    }
    In small programs like the above which is the best way to exit after an error ?
    return or exit() or it doesn't matter which one to choose ?

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I would say, definitely return. It would seem that exit() means you are beginning to panick, and abort() means you are scared to death. In case of normal execution flow you should let the program terminate normally (return).
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User ch4's Avatar
    Join Date
    Jan 2007
    Posts
    154
    Thank you

  4. #4
    Registered User
    Join Date
    Nov 2008
    Location
    My computer
    Posts
    65
    Actually, when there is an error abort() is for panicking, my vote would be for exit().

  5. #5
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    If you're in the main() function what difference does it make? return, exit() & abort() will all end the program. exit() & abort() are only needed if you want to end the program from a function other than main() (or if you still have some cleanup to do that you registered with the atexit() function).
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. Alegro closes out on me
    By campsoup1988 in forum C++ Programming
    Replies: 8
    Last Post: 04-03-2006, 10:40 AM
  3. C++ FTP class won't work
    By lord mazdak in forum C++ Programming
    Replies: 8
    Last Post: 12-18-2005, 07:57 AM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. OpenGL and Windows
    By sean345 in forum Game Programming
    Replies: 5
    Last Post: 06-24-2002, 10:14 PM