Computer >> Máy Tính >  >> Lập trình >> lập trình C

Viết chương trình C để kiểm tra hàm atexit ()

Atexit () là một hàm cho phép người dùng đăng ký một hàm phải được gọi khi kết thúc chương trình.

Đây là một hàm được xác định trước được bao gồm trong các tệp tiêu đề stdlib.

Ví dụ 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;
}

Đầu ra

Welcome to New,World

Ví dụ 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;
}

Đầu ra

This is a beautiful,Wonderful life