Storage Class

In addition to the data type, which indicates the binary format of the data, each variable also has a storage class, which indicates where in memory the data are stored and how that memory is managed.


The default storage class for local variables (variables defined inside the main program or inside any other subprogram) is auto.

The term "auto" comes from C, but the same concept applies to Fortran and most other languages.

Auto variables use memory space on the system stack. When a program enters a new block of code where there are variable definitions, it allocates another block of memory on top of the stack, on top of already existing variables that have been allocated previously.

When the program exits that block, the space allocated for auto variables is immediately released. I.e., the top of the stack reverts to what it was before the block was entered.

The stack:

|   Auto variables in subprog2, called by subprog1      |
|   Auto variables in subprog1, called by main program  |
|   Auto variables in main program                      |

This strategy is very efficient, since allocation and deallocation are simple, and the minimum amount of stack space is in use at any given time.

Since auto variables are allocated when the code block is entered, initializers on auto variables cause the variable to be reinitialized every time the code block is entered, as if the variable definition were immediately followed by an assignment statement.


Static variables are allocated at compile-time and remain allocated throughout the existence of a process.

The static storage class is the default for global variables in C, i.e. variables defined outside of any function including the main program. ( Again, global variables should generally not be used, since they cause side-effects, which make the program difficult to debug. )

In C, local variables can be defined as static by adding the static modifier to the definition:

static double   radius;

Fortran, being a somewhat more abstract language than C, uses the save modifier to indicate static storage class. This indicates that the variable's content should be saved across subprogram calls. It says more about how the variable is used than how it works behind the scenes.

real(8), save :: radius

Since a static variable defined this way remains allocated throughout the life of the process, it retains its value after the subprogram exits and will have the same value next time the subprogram is called.

Recall that auto variables are deallocated when the program exits the block in which they are defined, so they may contain garbage left over from other uses of that memory location (e.g. auto variables in other code blocks) the next time the block is entered.

Since static variables are allocated at compile-time, initializers on static variables cause the variable to be initialized only when the process is first created.


The C language also has a a register storage class, which attempts to keep the variable's content in a CPU register at all times. Since CPU registers are very few, this cannot be guaranteed.

In theory, this can greatly improve performance, since a register can be accessed within a single clock cycle, while memory access may require waiting many clock cycles.

However, modern compilers have intelligent optimizers that can use precious CPU registers to temporarily cache many different variables, each when it matters most. This strategy is far more effective in improving program performance than keeping a few specific variables in registers at all times.

Hence, the register storage class is widely regarded as obsolete.


Storage space for variables can also be manually allocated and freed by the programmer. Memory allocated this way uses another area of memory known as the heap. We will discuss this in detail in the section called “Dynamic Memory Allocation”.

  1. What are the 4 storage classes in C? Which one does Fortran not support?
  2. When is memory allocated for an auto variable? When is it freed?
  3. When is memory allocated for a static variable? When is it freed?
  4. Why would we use a static variable in a subprogram?
  5. When should we use the register storage class?