Node:Scope example, Next:, Previous:Communication via parameters, Up:Scope



Scope example

Notice that there are two variables named my_var in the example below, both visible in the same place. When two or more variables visible in one area of code have the same name, the last variable to be defined takes priority. (Technically adept readers will realize that this is because it was the last one onto the variable stack.)

/***************************************************************/
/*                                                             */
/* SCOPE                                                       */
/*                                                             */
/***************************************************************/

#include <stdio.h>

int main ()
{
  int my_var = 3;

  {
    int my_var = 5;
    printf ("my_var=%d\n", my_var);
  }

  printf ("my_var=%d\n", my_var);

  exit(0);
}

When you run this example, it will print out the following text:

my_var=5
my_var=3