views:

331

answers:

9

In C/C++ language loop statements we use exit(0), or exit(1) or other values. What is needed of that value, what is the role of that value in a loop when we exit the loop, and what is the meaning of 1 and 0 in exit()?

+10  A: 

exit() will terminate the process, not the loop. For the argument, it's the exit status (0, EXIT_SUCCESS, EXIT_FAILURE) : http://www.opengroup.org/onlinepubs/000095399/functions/exit.html

A suggestion : you should search and read the documentation of functions or language feature before asking.

Klaim
A: 

At the risk of sounding redundant, its the exit code for the process. Typically, you can define a range of values indicating different degrees of success or failure. Traditionally, 0 is success and 1 (or just non-zero) is failure. Then the program or function that invoked yours can examine the value (if its so inclined) and glean from it some idea as to whether or not your process was successful.

For example, you could have a program that copies a file from location A to B. If you are unable to copy the file because location B is write-protected, you could return -1, and any other program that utilizes yours as a step in its process now knows that you failed, and that expecting the file at location B to be complete and accessible is a bad idea.

GWLlosa
A: 

You don't care to know if you are in a loop or not if you use the exit() function. It will quit the program and return the given integer as the return value.

There is no loop return value or such thing in C/C++.

Patrick MARIE
Can the downvote here be explained ?
Patrick MARIE
A: 

The value only matters if an external program is going to use it to determine if the program ran successfully or not. Usually a program that runs successfully returns 0 or greater, and if it fails it returns -1.

kitchen
+4  A: 

You don't use exit() to exit a loop. exit will exit your whole program.

The number you supply to the exit function will be the exit code of your program.

Typically an exit value of 0 indicates that the program finished successfully.

A non-zero value is usually an error identifier for the program, used to indicate that

  1. The program failed
  2. the number usually indicates why it failed
Glen
A: 

For hysterical ... sorry, historical ... reasons.

Going back to Unix days, and still in Linux, Windows, etc, etc, you program does not just run alone - it gets called from somewhere (another program, the shell (bash, etc, dos prompt, etc) and they might want to know if it succeeded or not...

Mawg
+1  A: 

Ummm...no, you have this badly wrong.

In Cish, exit terminates the entire program. If you just want to stop looping, you use break. If you want to stop the current loop iteration and proceed to the next one, you use continue.

The value supplied to exit is returned as the exit value of your entire program (like you had returned that value from main). What the OS does with that value is up to the OS. What it means is also more or less up to you, although some OS'es define anything other than 0 as an error for various utilities.

T.E.D.
A: 

You may be confusing return(x) and exit(x). return sends info back to the calling function, exit shuts down the whole process, giving an exit status to the environment that started the process (if any). The values returned from functions in C are often error values, but their meaning depends on the semantics of the process; the meaning of exit depends on the semantics of the enviroment.

Think of it like a ship; return is like a message given to an immediate supervisor, and might indicate why the decks couldn't be cleaned today, and why the project was abandoned; exit is a message corked up in a bottle to describe why and how the ship sank.

Tim Schaeffer
+1  A: 

Popular statements for terminating a language loop are: break, continue, goto, return, throw and exit. There are other functions that can exit a loop, but you can research them yourself.

The break statement exits the nearest loop. Execution resumes after the end of the loop.

The continue statement cause execution to start at the top of the loop. Statements after continue will not be executed. This may or may not exit a loop, depending on your pedantics.

The goto statement can be used to exit a loop. Place a label outside of the loop and use goto.

The return statement will exit the function. If the function is main, it will terminate the program.

The throw statement will exit the loop and the current function. Execution resumes at the nearest catch statement or terminates the program if no relevant catch statements are found.

The exit function will exit a loop and terminate a program. The values passed to exit will be passed to the Operating System after the program terminates. Some OSes allow the return value of a program to be used in a script. The values of 0 and 1 for exit are OS dependent; some use 0 to indicate successful termination. Other values may give reasons for the termination.

Other methods to exit a loop, some depend on implementation of undefined behavior:

  1. Satisfy the loop's terminating condition.
  2. Divide by zero.
  3. Dereference a null pointer.
  4. Square root of a negative number.
  5. Calculate the length of a C-string without a terminating NUL character.

If you clarify your question, you will get better answers.

Thomas Matthews