[ First ]  [ Previous ]  [ Next ]  [ Last ]  [ Manuals ]


atexit

Install a function to be executed at a program's exit.

Compatibility:

This function is compatible with the following targets:

ANSI

BeOS

EMB/RTOS

Mac OS

Palm OS

Win32


Prototype:
#include <stdlib.h>
int atexit(void (*func) void));
Parameters:

Parameters for this facility are:

func  
void *  
The function to execute at exit  

Remarks:

The atexit() function adds the function pointed to by func to a list. When exit() is called, each function on the list is called in the reverse order in which they were installed with atexit(). After all the functions on the list have been called, exit() terminates the program.

The stdio.h library, for example, installs its own exit function using atexit(). This function flushes all buffers and closes all open streams.

Return:

atexit() returns a zero when it succeeds in installing a new exit function and returns a nonzero value when it fails.

See Also:

"exit"

Example of atexit() usage.:
#include <stdlib.h>
#include <stdio.h>

// Prototypes 
void first(void);
void second(void);
void third(void);

int main(void)
{
	atexit(first);
	atexit(second);
	atexit(third);
	
 	printf("exiting program\n\n");
	return 0;
}

void first(void)
{
	int c;

	printf("First exit function.\n");
	printf("Press return.\n");
// wait for the return key to be pressed
	c = getchar();
}

void second(void)
{
	int c;

	printf("Second exit function.\n");
	printf("Press return.\n");
	c = getchar();
}

void third(void)
{
	int c;

	printf("Third exit function.\n");
	printf("Press return.\n");
	c = getchar();
}

Output:
Third exit function.
Press return.

Second exit function.
Press return.

First exit function.
Press return.


[ First ]  [ Previous ]  [ Next ]  [ Last ]  [ Manuals ]

Visit the Metrowerks website at: http://www.metrowerks.com
For assistance contact Metrowerks Technical Support at: cw_support@metrowerks.com
Copyright © 2000, Metrowerks Corp. All rights reserved.

Last updated: August 16, 2000