atexit() เป็นฟังก์ชันที่อนุญาตให้ผู้ใช้ลงทะเบียนฟังก์ชันที่ต้องถูกเรียกตามการยุติโปรแกรม
เป็นฟังก์ชันที่กำหนดไว้ล่วงหน้าซึ่งรวมอยู่ในไฟล์ส่วนหัว stdlib
ตัวอย่างที่ 1
#include<stdio.h>
#include<stdlib.h>
void welcome(void){
printf("Welcome to New,");
}
void world(void){
printf("World\n");
}
int main(){
//test atexit ,call user defined function
atexit(world);
atexit(welcome);
return 0;
} ผลลัพธ์
Welcome to New,World
ตัวอย่างที่ 2
#include<stdio.h>
#include<stdlib.h>
void first(void){
printf("This is a beautiful,");
}
void second(void){
printf("Wonderful life\n");
}
int main(){
//test atexit ,call user defined function
atexit(second);
atexit(first);
return 0;
} ผลลัพธ์
This is a beautiful,Wonderful life