Page 193 - C-Language
P. 193

foo += 2;
         }

         int main(void)
         {
             foo = 1;

             test_function();
             printf("%d\r\n", foo); //foo = 3;

             return 0;
         }


        Function scope


        Function scope is the special scope for labels. This is due to their unusual property. A label is
        visible through the entire function it is defined and one can jump (using instruction gotolabel) to it
        from any point in the same function. While not useful, the following example illustrate the point:


         #include <stdio.h>

         int main(int argc,char *argv[]) {
             int a = 0;
             goto INSIDE;
           OUTSIDE:
             if (a!=0) {
                 int i=0;
               INSIDE:
                 printf("a=%d\n",a);
                 goto OUTSIDE;
             }
         }


        INSIDE may seem defined inside the if block, as it is the case for i which scope is the block, but it
        is not. It is visible in the whole function as the instruction goto INSIDE; illustrates. Thus there can't
        be two labels with the same identifier in a single function.


        A possible usage is the following pattern to realize correct complex cleanups of allocated
        ressources:


         #include <stdlib.h>
         #include <stdio.h>

         void a_function(void) {
            double* a = malloc(sizeof(double[34]));
            if (!a) {
               fprintf(stderr,"can't allocate\n");
               return;                 /* No point in freeing a if it is null */
            }
            FILE* b = fopen("some_file","r");
            if (!b) {
               fprintf(stderr,"can't open\n");
               goto CLEANUP1;          /* Free a; no point in closing b */
            }
            /* do something reasonable */
            if (error) {



        https://riptutorial.com/                                                                             169
   188   189   190   191   192   193   194   195   196   197   198