Thread: difference

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    9

    difference

    what is the difference between exit(0) and exit(1)?
    is it same??

  2. #2
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    Both halt the program, but return different values to the OS.

    Generally, when a program ends, it returns an exit value to the OS. Traditionally an exit value of zero tells the OS that the program ran to completion without problems. Nonzero values tell it that something went wrong and the program halted early, with each different non-zero value corresponding to a different thing that went wrong in the program. For example 1 might mean that you didn't give it enough command line arguments, 2 might mean that it couldn't allocate enough memory, etc.
    Code:
    while(!asleep) {
       sheep++;
    }

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    what TheBigH said. one effect of returning the 0 or nonzero error codes is that if you don't do it right (or for example declare 'void main(void)', your program won't work right in 'make' and scripts that check return codes. even if you aren't using make or scripts now, its good practice to do it right.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by dmh2000 View Post
    what TheBigH said. one effect of returning the 0 or nonzero error codes is that if you don't do it right (or for example declare 'void main(void)', your program won't work right in 'make' and scripts that check return codes. even if you aren't using make or scripts now, its good practice to do it right.
    I don't know what you mean by "make scripts"... but you are on the right track.

    I'm more familiar with windows, so I'll give an examle from there...

    In batch files the returned value from a program is made available in the ERRORLEVEL value which can be tested in the IF statement, allowing batch files to branch according to the returned values from programs.

    Similarly when a "child" program is launched with CreateProcess() the returned value from the program can be inspected and is made available in the GetExitCodeProcess() function. This can be used to test the result of running the program.

    So void main() just doesn't cut it now... in fact it didn't cut it then either. Even back in the good old DOS days I sometimes had batch files go silly on me because a program wasn't returning what it should.

    The thing to keep in mind is that with void main() your program may run just fine but it will not be correctly interfaced to the operating system. The exit(errorlevel) and return errorlevel; functions do have meaning to the OS. If you don't use them the OS gets garbage and programs that rely on those values may behave erratically.
    Last edited by CommonTater; 12-07-2011 at 02:46 PM.

  5. #5
    Registered User
    Join Date
    Dec 2011
    Posts
    9
    thank you...

  6. #6
    Registered User ledow's Avatar
    Join Date
    Dec 2011
    Posts
    435
    If in doubt, if you have a define of EXIT_SUCCESS or EXIT_FAILURE available on your system, return those. GCC supports them, for instance:

    Code:
    if(it_worked)
    {
        return(EXIT_SUCCESS);
    }
    else
    {
        return(EXIT_FAILURE);
    };
    If you run your code via something like Eclipse or another IDE, generally the program will be considered to have "failed" if it didn't return 0 (EXIT_SUCCESS). Not usually important, but for future reference, it's good to know why.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by ledow
    If in doubt, if you have a define of EXIT_SUCCESS or EXIT_FAILURE available on your system, return those. GCC supports them
    Those are actually standard macros available via <stdlib.h>
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Difference?
    By RBCC in forum C Programming
    Replies: 5
    Last Post: 10-27-2010, 05:37 PM
  2. Difference between C++ and VC++?
    By #include Jack in forum C++ Programming
    Replies: 55
    Last Post: 03-31-2010, 04:31 PM
  3. difference between **v and v[][]
    By ignazioc in forum C Programming
    Replies: 10
    Last Post: 02-24-2010, 04:39 PM
  4. what is the difference?
    By planet_abhi in forum C++ Programming
    Replies: 1
    Last Post: 11-07-2003, 10:23 AM
  5. What's the Difference?
    By jrahhali in forum Networking/Device Communication
    Replies: 4
    Last Post: 08-17-2003, 04:49 PM