Page t048, go to NEXT, PREVIOUS, INDEX

Scope, Initialization and Lifetime of Variable


Consider

      int main ()
      {
        float temp = 1.1;
        int a;
        int b;
        printf ("Value for a en b [int]: ");
        scanf ("%d%d", &a, &b);

        if ( a < b )
        { int temp = a;  /* this "temp" hides the other one */
          printf ("Smallest local ""temp"" = a*2 = %d\n", 2*temp);
        }  /* end of block; local "temp" deleted */
        else
        { int temp = b;  /* another "temp" hides the other one */
          printf ("Smallest local ""temp"" = b*3 = %d\n", 3*temp);
        }  /* end of block; other local "temp" deleted */

        printf ("Global ""temp"" used: %f\n", a * b + temp);

        return 0;
      }

C Course, 22-jan-1997, Peter Klok, pfk@hef.kun.nl