Javatpoint Logo
Javatpoint Logo

Difference Between exit() and return() in C

In the C programming language, both exit() and return() are used to terminate the execution of a program or a function, but they serve different purposes and have different behaviours. In this article, we will explore the difference between exit() and return() in C.

exit():

The exit() function is primarily used to terminate the entire program and return control to the operating system. It is typically called when the program has finished its execution or when an unrecoverable error occurs. exit() allows the program to gracefully terminate and perform cleanup activities before exiting. It can be used to release resources, close open files, and execute registered functions via atexit(). When exit() is called, the program control does not return to the point where exit() was invoked. Instead, the control is handed back to the operating system, and the program exits with a status code. This status code can be retrieved by the calling environment and is typically used to indicate the success or failure of the program.

return():

The return statement is used within a function to terminate the execution of the function and return a value back to the caller. It is used to indicate the end of the function's execution and provide a result (if the function has a non-void return type). return() allows for precise control of program flow within a function. It can be called at any point within a function to exit the function and return a specific value. Multiple return statements can be used within a function, each with a different return value based on different conditions. When return is encountered, the function's execution is terminated, and the control returns to the caller function. The caller function continues its execution from the point where the function was called. Unlike exit(), return() does not terminate the entire program but rather allows the program to continue executing with the returned value.

1. Purpose:

  • exit(): The exit() function is used to terminate the entire program and return control to the operating system. It is typically called when the program has finished its execution or when an unrecoverable error occurs. It allows the program to gracefully terminate and clean up resources before exiting.
  • return(): The return statement is used within a function to terminate the execution of the function and return a value back to the caller. It is used to indicate the end of the function's execution and provide a result (if the function has a non-void return type).

2. Usage:

  • exit():
    • Syntax: void exit(int status);
    • exit() is typically called from the main() function or any other function to terminate the entire program.
    • It is included in the <stdlib.h> header file.
  • return():
    • Syntax: return [expression];
    • return is used within a function to return a value to the caller function.
    • It can be used in any function that has a return type other than void.
    • It can be used multiple times within a function, each with a different return value.

3. Program Termination:

  • exit():
    • When exit() is called, it immediately terminates the program without executing any further statements.
    • It performs cleanup activities, such as closing open files, releasing dynamically allocated memory, and executing registered functions via atexit().
    • The control is handed back to the operating system, and the program exits with a status code that can be retrieved by the calling environment.
  • return():
    • When return is encountered within a function, it terminates the function's execution and returns control to the caller function.
    • The program continues execution from the point where the function was called.
    • It does not perform any cleanup activities, as it assumes the caller function will handle any necessary cleanup.

4. Value Passing:

  • exit():
    • exit() does not provide a mechanism to return a value directly to the caller function. Instead, it terminates the program and returns a status code to the calling environment.
    • The status code is an integer value that represents the exit status of the program, typically used to indicate success or failure.
  • return():
    • return is used to pass a value back to the caller function.
    • The value can be of any type that matches the return type specified in the function declaration.
    • The value is usually used for further processing or decision-making in the caller function.

5. Function Execution:

  • exit():
    • When exit() is called, the program immediately terminates, and all subsequent statements are skipped.
    • Functions that were in the middle of execution do not get a chance to complete their execution.
    • Any open files are automatically closed, and dynamically allocated memory is freed by the operating system.
  • return():
    • When return is encountered within a function, it terminates the function's execution, and the control returns to the caller function.
    • The caller function continues its execution from the point where the function was called.
    • Functions that were in the middle of execution complete their execution before returning to the caller function.

6. Flow Control:

  • exit():
    • exit() is primarily used for abnormal termination or when the program has finished its execution.
    • It does not allow for fine-grained control of program flow.
    • Once exit() is called, the program control does not return to the point where exit() was invoked.
  • return():
    • return is used to return control to the caller function explicitly.
    • It allows for precise control of program flow, as it can be called at any point within a function.
    • Multiple return statements can be used within a function to return different values based on different conditions.

To summarize, the main difference between exit() and return() in C is that exit() terminates the entire program, performs cleanup activities, and returns a status code to the operating system, while return() is used to exit a function, provide a return value to the caller function, and continue program execution from the point where the function was called.

Here's an example code snippet that demonstrates the usage of exit() and return() in C:

Output:

ERROR!
Result 1: 5
Error: Division by zero!

Explanation:

  • In this example, we have a divide() function that performs integer division between two numbers. The function checks if the divisor (b) is zero. If it is, an error message is printed, and the program is terminated using exit(1). This is an example of abnormal termination using exit().
  • In the main() function, we call divide() twice. The first call is divide(10, 2), which performs a valid division. The result is then printed. This is an example of normal termination using return.
  • The second call is divide(8, 0), which attempts to divide by zero. In this case, the if condition in the divide() function is true, and the error message is printed. The program is then terminated using exit(1). The subsequent line in the main() function (printf) is not executed because the program has terminated.
  • The first function call divide(10, 2) performs a valid division, where 10 is divided by 2. The result is 5, which is then printed in the main() function using printf. Hence, the output line "Result 1: 5" is displayed.
  • The second function call divide(8, 0) attempts to divide 8 by 0, which results in an error. The if condition in the divide() function detects the division by zero and prints the error message "Error: Division by zero!" using printf. After printing the error message, the exit(1) function is called, terminating the program abnormally with a status code of 1. As a result, the subsequent printf statement in the main() function is not executed.

Note: The status code 1 passed to exit() indicates that the program terminated abnormally due to an error.

Conclusion:

In this example, exit() is used to handle an unrecoverable error condition. When exit() is called, the program terminates immediately, and the control is returned to the operating system. It does not allow the program to continue executing from where the error occurred.

On the other hand, return is used to exit the divide() function normally and return a result back to the caller function (main() in this case). The program execution continues after the function call, allowing further statements to be executed.

Overall, exit() is suitable for abnormal terminations or when the program needs to terminate completely, while return() is used for regular function terminations and to pass values back to the caller function.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA