Automatic Variables

Local variables declared inside a function

Any variable declared as a parameter to a function or inside the body of the function are automatic by default (as shown in the code snippet). 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 that 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 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 an 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.

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

auto Keyword with Variables

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 next page) are much more efficient in terms of the amount of code required to access them. Therefore, compilers often have an 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.

© 2016 Microchip Technology, Inc.
Information contained on this site regarding device applications and the like is provided only for your convenience and may be superseded by updates. It is your responsibility to ensure that your application meets with your specifications. MICROCHIP MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND WHETHER EXPRESS OR IMPLIED, WRITTEN OR ORAL, STATUTORY OR OTHERWISE, RELATED TO THE INFORMATION, INCLUDING BUT NOT LIMITED TO ITS CONDITION, QUALITY, PERFORMANCE, MERCHANTABILITY OR FITNESS FOR PURPOSE. Microchip disclaims all liability arising from this information and its use. Use of Microchip devices in life support and/or safety applications is entirely at the buyer's risk, and the buyer agrees to defend, indemnify and hold harmless Microchip from any and all damages, claims, suits, or expenses resulting from such use. No licenses are conveyed, implicitly or otherwise, under any Microchip intellectual property rights.