与终止程序相关的函数

exit

 

Performs complete C library termination procedures, terminates the process, and exits with the supplied status code.

 

void exit( int status );

 

Required Header <stdlib.h>

Parameter  status   Exit status

Remarks

The exit terminates the calling process. exit calls, in last-in-first-out (LIFO) order, the functions registered by atexit and _onexit, then flushes all file buffers before terminating the process.The status value is typically set to 0 to indicate a normal exit and set to some other value to indicate an error.

Although the exit and _exit calls do not return a value, the low-order byte of status is made available to the waiting calling process, if one the waiting calling processexists, after the calling process exits. The status value is available to the operating-system batch command ERRORLEVEL and is represented by one of two constants: EXIT_SUCCESS, which represents a value of 0, or EXIT_FAILURE, which represents a value of 1.

 

atexit

Processes the specified function at exit.

int atexit( void ( __cdecl *func )( void ) );

Return Value

atexit returns 0 if successful, or a nonzero value if an error occurs.

Parameter

func Function to be called

Remarks

The atexit function is passed the address of a function (func) to be called when the program terminates normally. Successive calls to atexit create a register of functions that are executed in LIFO (last-in-first-out) order. The functions passed to atexit cannot take parameters. atexit and _onexit use the heap to hold the register of functions. Thus, the number of functions that can be registered is limited only by heap memory.

Example

/* ATEXIT.C: This program pushes four functions onto

 * the stack of functions to be executed when atexit

 * is called. When the program exits, these programs

 * are executed on a "last in, first out" basis.

 */

 

#include <stdlib.h>

#include <stdio.h>

 

void fn1( void ), fn2( void ), fn3( void ), fn4( void );

 

void main( void ){

   atexit( fn1 );

   atexit( fn2 );

   atexit( fn3 );

   atexit( fn4 );

   printf( "This is executed first./n" );

}

 

void fn1(){

   printf( "next./n" );

}

 

void fn2(){

   printf( "executed " );

}

 

void fn3(){

   printf( "is " );

}

 

void fn4(){

   printf( "This " );

}

 

Output

This is executed first.

This is executed next.

 

abort

Aborts the current process and returns an error code.

void abort( void );

Return Value

abort does not return control to the calling process. By default, it terminates the current process and returns an exit code of 3.

Remarks

The abort routine prints the message “abnormal program termination” and then calls raise(SIGABRT). The action taken in response to the SIGABRT signal depends on what action has been defined for that signal in a prior call to the signal function. The default SIGABRT action is for the calling process to terminate with exit code 3, returning control to the calling process or operating system. abort does not flush stream buffers or do atexit/_onexit processing.

abort determines the destination of the message based on the type of application that called the routine. Console applications always receive the message via stderr. In a single or multithreaded Windows application, abort calls the Windows MessageBox API to create a message box to display the message along with an OK button. When the user selects OK, the program aborts immediately.

When the application is linked with a debug version of the run-time libraries, abort creates a message box with three buttons: Abort, Retry, and Ignore. If the user selects Abort, the program aborts immediately. If the user selects Retry, the debugger is called and the user can debug the program if Just-In-Time (JIT) debugging is enabled. If the user selects Ignore, abort continues with its normal execution: creating the Example

/* ABORT.C:  This program tries to open a

 * file and aborts if the attempt fails.

 */

 

#include  <stdio.h>

#include  <stdlib.h>

 

void main( void ){

   FILE *stream;

 

   if( (stream = fopen( "NOSUCHF.ILE", "r" )) == NULL ){

      perror( "Couldn't open file" );

      abort();

   }

   else

      fclose( stream );

}

 

Output

Couldn't open file: No such file or directory

 

abnormal program termination

 

你可能感兴趣的:(function,Stream,application,Signal,returning,Constants)