C Programming Automatic Variables

Last modified by Microchip on 2023/11/10 10:59

Local Variables Declared Inside a Function

Any variable declared as a parameter to a function or inside the body of the function is automatic by default. An automatic variable is created on the stack when the function is called, and it is destroyed when exiting from the function. This means that they only exist as long as the function is active so if a function is called repeatedly during a program's execution, the parameters and variables will be freshly allocated, brand new entities that know nothing about their previous existence (if any). Therefore, any value they may be assigned during the execution of the function will be lost when returning from the function call.

auto Keyword Usually Not Required – Local Variables are Automatically Automatic

According to most books on C, the auto keyword serves no purpose whatsoever since it can only be used inside functions (not applicable to global variables) and those parameters and local variables are automatic by default. While this may be true in the world of the PC and mainframe, embedded systems are a little different.

While most embedded C compilers do make parameters and local variables automatic by default, they will often have the option to make them static by default. The advantage of static variables is that it takes less code space to access them. The disadvantage is that they always exist, even when the function is not in scope, and therefore take up RAM. If the default static option is selected (this is a setting available on some compilers, like the older MPLAB® C18), then the auto keyword may be used to override that option in cases where having an automatic variable would be advantageous.

Typically Created on the Stack

Most compilers allocate automatic variables and parameters on the software stack. However, due to the unique architectures of some embedded processors, this is not always the case. For example, MPLAB XC16 passes function parameters in the working registers of the PIC24 or dsPIC® instead. If there aren't enough working registers to accommodate the parameters, then the software stack is used. There are a few other cases where the stack might be used instead – see the MPLAB XC16 User's Guide for details. MPLAB C18, on the other hand, handles parameters in the usual way via the software stack – unless the default static option is enabled.

1
2
3
4
int foo(int x, int y)
{
 int a, b;
  ...

In the above example x, y, a, and b are all automatic variables.

auto Keyword With Variables

int foo(auto int x, auto int y)
{
  ... ;
}

auto is No Longer Used by Most Compilers

Since function parameters and local variables are usually automatic by default, the auto keyword is almost never used. This is particularly true in the world of PC programming. However, in the embedded world, auto still has its uses. As mentioned before, on some architectures, static variables (global variables with a permanent address – discussed on the "Static Variables" page) are much more efficient in terms of the amount of code required to access them. Therefore, compilers often have the option to make parameters and local variables static by default. If this is the case, auto is useful to make individual variables automatic in circumstances where stack allocation is more advantageous for a particular variable while leaving all others static.