Exit() function in C
×


Exit() function in C

31

The exit function in C is used to terminate a program execution immediately.

It allows you to exit a program gracefully by returning a status code to the operating system.

Syntax:

void exit(int status);

status: An integer value representing the exit status of the program.


Types of exit

1 Normal Exit: exit(0) or exit(EXIT_SUCCESS)

2 Abnormal Exit: exit(EXIT_FAILURE)

Examples:

1 Normal Exit

#include<stdio.h>
#include<stdlib.h> 

int main() {
    printf("Program will exit normally.\n");
    exit(EXIT_SUCCESS);
    printf("This line won't be executed.\n");
}
	

Output:

Program will exit normally.

2 Abnormal Exit

#include<stdio.h>
#include<stdlib.h> 

int main() {
    printf("Program will exit abnormally.\n");
    exit(EXIT_FAILURE);
    printf("This line won't be executed.\n");
}
	

Output:

Program will exit abnormally.

Importance of Exit Function in C

Graceful Termination: Allows the program to shut down in an organized manner.

Cleanup: Useful for performing cleanup tasks like closing files or freeing memory before exiting.

Exit Status: Provides a way to communicate the exit status to the calling environment or parent process.



Best WordPress Hosting


Share:


Discount Coupons

Get a .COM for just $6.98

Secure Domain for a Mini Price



Leave a Reply


Comments
    Waiting for your comments