English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

C / C ++the exit(), abort(), and assert() in the

exit()

The functionexit()Used to immediately terminate the calling function without executing further processing. Asexit()Function call that terminates the process. Declared in the 'stdlib.h' header file. It does not return anything.

This isexit()The syntax of C language,

void exit(int status_value);

Here,}

status_value-the value returned to the parent process.

This isexit()Example in C language,

Example

#include <stdio.h>
#include <stdlib.h>
int main() {
   int x = 10;
   printf("The value of x: %d\n", x);
   exit(0);
   printf("Calling of exit()");
   return 0;
}

Output results

The value of x: 10

In the above program, the variable "x" is initialized with a value. Print the value of the variable andexit()call the function. Asexit()as called, it immediately exits execution and does not printprintf()callexit()as follows-

int x = 10;
printf("The value of x: %d\n", x);
exit(0)

abort()

The functionabort()abnormally terminates execution. It is recommended not to use this feature to terminate. It is declared in the "stdlib.h" header file.

This isabort()The syntax of C language,

void abort(void);

This isabort()Example in C language,

Example

#include <stdio.h>
#include <stdlib.h>
int main() {
   int a = 15;
   printf("The value of a: %d\n", a);
   abort();
   printf("Calling of abort()");
   return 0;
}

This is the output,

Output results

The value of a: 15

In the above program, the variable "a" is initialized with this value and printed. Whenabort()is called when it immediately but abnormally terminates execution. The callabort()as follows.

int a = 15;
printf("The value of a: %d\n", a);
abort();

assertion()

The functionassert()is declared in the "assert.h" header file. It calculates the expression given as a parameter. If the expression is true, no operation is performed. If the expression is false, execution is terminated.

This isassert()The syntax of C language,

void assert(int exp);

here.

exp-the expression you want to evaluate.

This isassert()Example in C language,

Example

#include <stdio.h>
#include <assert.h>
int main() {
   int a = 15;
   printf("The value of a: %d\n", a);
   assert(a !=15);
   printf("Calling of assert()");
   return 0;
}

Output results

The value of a: 15
main: main.c:9: main: Assertion `a !=15' failed.

In the above program, the variable "a" is initialized with a value. Print the value of the variable andassert()call the function. Asassert()When called, it calculates the expression "a" as not equal to15It is false, which is why it also interrupts execution and displays an error message.

int a = 15;
printf("The value of a: %d\n", a);
assert(a !=15);