1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18

Summary:

  1. local variables can only be seen within their procedure
  2. by default global variables can be seen everywhere
  1. statically defined global variables can only be seen in
    the current file
  2. external variables must be declared


Storage Allocation and Storage Classes

  • By default local variables have the storage class auto,
    they are allocated on the stack when the procedure is
    called and their memory is released when the procedure
    returns - the values of auto variables are not saved
    between calls
  • A static local variable is not allocated on the stack, it is
    allocated in the data segment, the memory for a static
    variable is allocated by the compiler, the value of a static
    variable is saved when its procedure returns, when the
    procedure is called again it will still have the same value,
    for example:


int count(void) {
static int calls = 1;

return(calls++);

}


March3, 1998

Page 17

C201/TAM