Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
An example of using long jumps in C to jump interprocedurally
#include <stdio.h>
#include <setjmp.h>
// saves the stack context/environment for nonlocal gotos
jmp_buf env;
// foo immediately makes a long jump to the setjmp in main
void foo(void){
// prints "hello cruel world!"
int main(int argc, char *argv[]){
// setjmp returns 0 if returning directly and nonzero
// when returning from a longjmp or siglongjmp using
// the saved stack context
if (!setjmp(env)){
printf(" sane world!\n");
return 0;
} else {
printf(" cruel world!\n");
return 1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment