Chapter Contents

Previous

Next
Function Categories

Program Control Functions

The program entry mechanism, which is the means by which the main function gains control, is system dependent. However, program exit is not always system dependent, although it does have some implementation dependencies.

One simple way to terminate execution of a C program is for the main function to execute a return statement; another is for the main function to drop through its terminating brace. However, in many cases, a more flexible program exit capability is needed. This capability is provided by the exit function described in this section. This function offers the advantage of allowing any function (not just main ) to terminate the program, and it allows information to be passed to other programs. (footnote 1)

You can use the atexit function to define a function to be called during normal program termination, either due to a call to exit or due to return from the main function.

The abend and abort functions can also be used to terminate execution of a C program. These functions cause abnormal termination, which causes both library cleanup and user cleanup (defined by atexit routines) to be bypassed.

In some cases, it is useful for a program to pass control directly to another part of the program (within a different function) without having to go through a long and possibly complicated series of function returns. The setjmp and longjmp functions

provide a general capability for passing control in this way.

You can use the SAS/C extension blkjmp to intercept calls to longjmp that cause the calling routine to be terminated. This is useful for functions that allocate resources that must be released before the function is terminated. You can also use blkjmp to intercept calls to exit .

Note:    The jump functions use a special type, jmp_buf , which is defined in the <setjmp.h> header file.  [cautionend]

Several of the program control functions have a special version for use in the Systems Programming Environment. See Implementation of Functions for more details.

The program control functions are
abend abnormally terminate execution via ABEND
abort abnormally terminate execution
atexit register program cleanup function
blkjmp intercept nonlocal gotos
exit terminate execution
longjmp perform nonlocal goto
onjmp define target for nonlocal goto
onjmpout intercept nonlocal gotos
setjmp define label for nonlocal goto.


FOOTNOTE 1:   For programs using the compiler indep feature, program execution can also be terminated by calling the L$UEXIT routine from non-C code, as described in Appendix 5, "Using the indep Option for Interlanguage Communication," in the SAS/C Compiler and Library User's Guide[arrow]


Chapter Contents

Previous

Next

Top of Page

Copyright © 2001 by SAS Institute Inc., Cary, NC, USA. All rights reserved.