Abort() function in C
×


Abort() function in C

187

 The abort() function in C is used to terminate a program immediately.

 It doesn't perform any cleanup operations and causes abnormal termination of the program.

 It's typically invoked when an unrecoverable error condition occurs.

Declaration:

void abort(void);

Implementation:

The actual implementation of abort() function may vary across different platforms and compilers.

It usually involves sending a termination signal to the operating system.

Examples:

Program for Basic Usage

//program for basic usage in C
#include<stdio.h>	
#include<stdlib.h>		

int main() {
    int x = 5;
    if (x == 5) {
        printf("Error: x cannot be 5\n");
        abort();
    }
    printf("Program continues after abort()\n");
    return 0;
}			

Output:

ERROR!
Error: x cannot be 5
Aborted

Program Using abort() in Error Handling

// program for using abort() in Error Handling
#include<stdio.h>	
#include<tdlib.h>		

int main() {
    int x;
    printf("Enter a positive number: ");
    scanf("%d", &x);
    
    if (x <= 0) {
        printf("Error: Entered number is not positive\n");
        abort();
    }
    
    printf("You entered: %d\n", x);
    return 0;
}			

Output:

Enter a positive number: 7
You entered: 7


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