Name

atexit - registers a function to be called at normal program termination

Library

libc.lib

Synopsis

  #include <stdlib.h>
  int atexit (void (*function)(void));

Return values

The atexit function returns the value 0 if successful; otherwise the value -1 is returned and the global variable errno is set to indicate the error.

Detailed description

The atexit function registers the given function to be called at program exit, whether via exit or via return from the program’s main. Functions so registered are called in reverse order; no arguments are passed.

These functions must not call exit; if it should be necessary to terminate the process while in such a function, the _exit function should be used. (Alternatively, the function may cause abnormal process termination, for example by calling abort

At least 32 functions can always be registered, and more are allowed as long as sufficient memory can be allocated.


Examples

#include <stdlib.h>
#include <stdio.h>
 
void fun1( void ), fun2( void ), fun3( void ), fun4( void );
 
int main( void )
{
   atexit( fun1 );
   atexit( fun2 );
   atexit( fun3 );
   atexit( fun4 );
   printf( "Before exiting....\n" );
}
 
void fun1()
{
   printf( " fun1\n " );
}
 
void fun2()
{
   printf( " fun2\n " );
}
 
void fun3()
{
   printf( " fun3\n " );
}
 
void fun4()
{
   printf( " fun4\n " );
}

         

Output

Before exiting...
fun4
fun3
fun2
fun1

         

Errors

[ENOMEM]
  No memory was available to add the function to the list. The existing list of functions is not modified.

See also

exit

Feedback

For additional information or queries on this page send feedback

© 2005-2007 Nokia

Top