9.7. Non-local jumpsProvision is made for you to perform what is, in effect,
a The file #include <setjmp.h> int setjmp(jmp_buf env); void longjmp(jmp_buf env, int val); The
#include <stdio.h>
#include <stdlib.h>
#include <setjmp.h>
void func(void);
jmp_buf place;
main(){
int retval;
/*
* First call returns 0,
* a later longjmp will return non-zero.
*/
if(setjmp(place) != 0){
printf("Returned using longjmp\n");
exit(EXIT_SUCCESS);
}
/*
* This call will never return - it
* 'jumps' back above.
*/
func();
printf("What! func returned!\n");
}
void
func(void){
/*
* Return to main.
* Looks like a second return from setjmp,
* returning 4!
*/
longjmp(place, 4);
printf("What! longjmp returned!\n");
}Example 9.3The If there has been no call to The It's a serious error to The Standard insists that, apart from appearing as the only expression
in an expression statement, setjmp(place); /* expression statement */ if(setjmp(place)) ... /* whole controlling expression */ if(!setjmp(place)) ... /* whole controlling expression */ if(setjmp(place) < 4) ... /* whole controlling expression */ if(setjmp(place)<;4 && 1!=2) ... /* forbidden */ |
The C BookThis book is published as a matter of historical interest. Please read the copyright and disclaimer information. GBdirect Ltd provides up-to-date training and consultancy in C, Embedded C, C++ and a wide range of other subjects based on open standards if you happen to be interested. |
|
West Yorkshire Office
GBdirect Ltd
Training: 0800 651 0338 Please call between 0900 and 1700 (UK time) on Monday to Friday South East Regional Office
GBdirect Ltd
Training: 0800 651 0338 Please call between 0900 and 1700 (UK time) on Monday to Friday Please note: |