Linux environment Programming process (III): Inter-function jump

Source: Internet
Author: User

Introduction: When writing a program, often use the goto statement inside the function to jump, so that the error processing, if you want to jump between functions what to do? Use the setjmp and LONGJMP functions.

To show an example program:

#include <stdio.h> #include <stdlib.h> #include <setjmp.h>static void F1 (int, int, int, int), static void F2 (void); static jmp_buf jmpbuffer;static int Globval;intmain (void) {int autoval;register int regival;volatile int volaval;static int statval;globval = 1;autoval = 2;regival = 3;  Volaval = 4;statval = 5;if (setjmp (jmpbuffer)! = 0) {printf ("after longjmp:\n");p rintf ("global =%d, Autoval =%d, regival =%d, Volaval =%d, Statval =%d\n ", Globval, Autoval, Regival, Volaval, Statval); exit (0);} /* * Change variable after setjmp, buf before longjmp. */globval = 95;autoval = 96;regival = 97; Volaval = 98;statval = 99;f1 (Autoval, Regival, Volaval, statval);/*never returns*/exit (0);}  static VOIDF1 (int i, int j, int k, int l) {printf ("in F1 (): \ n");p rintf ("global =%d, Autoval =%d, Regival =%d, Volaval = %d, Statval =%d\n ", Globval,i, J, K, L); F2 (); Static voidf2 (void) {longjmp (Jmpbuffer, 1);}

To compile the program test results:

In F1 (): global = Autoval = Regival,  volaval = 98, Statval = 99after Longjmp:global = up, Autoval = +, re Gival  = Volaval = 98, Statval = 99


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.