/***************************************************************************
 *  Columbia University Introduction to Computer Programming in C COMS 1003
 *  Demonstration of different scopes in a program
 *
 *  Copyright (C) 2005 Michael E. Locasto
 *
 *  All rights reserved.
 *
 * $Id: scopes.c,v 1.1 2005/10/08 19:17:51 locasto Exp $
 **************************************************************************/

#include <stdio.h>

/* This long integer is declared outside the body of any function,
 * thus, it is of global scope and any function can manipulate it.
 */
long a_global_variable = 0;

/*
 * Since 'a_global_variable' is declared globally, function a() can
 * manipulate it. 
 */
long a(void)
{
   return a_global_variable * 2;
}

/**
 * Demonstrate declaring a local variable. No other function can
 * access *this* particular local variable named x, but look at
 * function c() below 
 */
long b(void)
{
   long x = 0L;
   return x;
}

/**
 * Re-use the variable name 'x', but in a different scope -- therefore,
 * there is no conflict.
 */
long c(void)
{
   long x = 4L;
   return x;
}

/**
 * With this function uncommented, the program will fail to compile
 * because you, the programmer, are attempting to use the same
 * variable name as two different variables (of different types)
 * in the same scope.
 */
/*
void d(double x)
{
   int x = 9;
   x++;
}
*/

/** 
 * Show that even declaring x as the same type results in a duplicate
 * definition. 
 */
/*
void dd(double x)
{
   double x = 66.0;
   x = x + 1.0;
}
*/

/**
 * This function demonstrates that the various branches of an if..else
 * block introduce a new scope.
 */
void e(void)
{
   int x = 0;
   x = 2;
   if(1)
   {
      int x = 5;
      x++;
   }else{
      int x = 10;
      x--;
   }
}

/**
 * Show how the same variable name actually refers to different
 * storage locations based on scope. Thus, just because a
 * variable may be indicated by an identical string in the program
 * text, you have to keep track of the scope of that variable as
 * well to make sure you are operating on the data you think you
 * are operating on.
 *
 * Of course, the other solution is to always chose to make your
 * variable names unique, but the question of scope still remains:
 * if you declare a local variable, you cannot use it outside of
 * the scope it is declared in.
 *
 * 
 */
int main(int argc, char* argv[])
{
   int foo, bar, jazz, buzz, temp;
   double q, average, r;

   foo++;
   bar++;
   jazz++;
   buzz++;
   temp++;
   q = 5.0;
   average = 34.688;
   average++;
   r = 1.0;

   return 0;
}
