Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call another function when main() exits

Tags:

c

main

Is it possible to call an extra function when main() exits in C?

Thanks!

like image 768
not34 Avatar asked Nov 21 '09 21:11

not34


People also ask

Is it possible to call at exit () function more than once in AC program?

In a C program, more than one break statement can be executed. In a C program, just one exit function will be executed.

How do you call a main function from another function?

In 'C' you can even call the main() function, which is also known as the "called function" of one program in another program, which is called "calling function"; by including the header file into the calling function.

How to call a function in c++ from main?

Calling a Function While creating a C++ function, you give a definition of what the function has to do. To use a function, you will have to call or invoke that function. When a program calls a function, program control is transferred to the called function.


1 Answers

You can register functions to run after main exits using the atexit function.

MSDN has a nice succinct example of how this is done. Basically, the functions registered with atexit are executed in reverse order of when they were registered.

like image 134
James McNellis Avatar answered Oct 05 '22 18:10

James McNellis